Home >Tips >PowerShell with SharePoint from Scratch >Upload files to SharePoint and move to subfolder kalmstrom.com site map icon Site map  

Upload files to SharePoint Library and Move to Subfolder

A PowerShell with SharePoint tutorial

PowerShell iconSharePoint has several methods to upload files from Windows Explorer to a document library, but if you want to do it automatically, you should use a PowerShell script.

In such a scenario, you can put all files that should be uploaded in a specific folder and then they will be uploaded automatically to the SharePoint library of your choice.

In the previous article, Peter Kalmström explainded how to create a script that uploads files from Windows Explorer to a SharePoint document library, and in next demo he will explain how to schedule such tasks.

However, first he must make sure that all files that have been copied to SharePoint will be moved to a subfolder. This subfolder, should of course not be uploaded to the library.

When the subfolder has been creted, Peter exoands the forEach loop that he created in the previous article. To filter out the subfolder, ´he uses the property psisContainer which returns a boolean that indicates whether the current object is a folder or not.

$Files = Get-ChildItem "C:\Users\PeterKalmström\Documents\ToImport"
foreach($File in $Files){
    #$File = $Files[0]
    if($File.PSIsContainer -ne $true){
        Add-PnPFile -Folder "Shared Documents" -Path $File.FullName
        Move-Item -Path $File.FullName -Destination  "C:\Users\PeterKalmström\Documents\ToImport\Imported"
    }
}
 



back icon next icon
Products Buy FAQ Services Tips Books Contact About Us Tools

Security and integrity

Copyright  Kalmstrom Enterprises AB  All rights reserved