Using PowerShell scripts to migrate content to SharePoint is a great option when you need flexibility. Scripts can include business rules for fine grain migration requirements, such as restructuring content or tagging.
I often use PnP PowerShell to automate migrations because of the flexibility it gives, but there is an issue. Performance can be very slow compared to other migration methods. Depending on the rules and complexity of the migration, performance is usually in the 1,000 to 3,000 documents per hour.
Parallel actions are a good way to improve performance. This works by running multiple instances (threads) of the commands inside a loop rather than completing each command sequentially. This capability is available in PowerShell 7.6 but not in older versions such as PowerShell 5.
This script is an example of a file upload process using PnP PowerShell. The code inside the “For Each” loop executes in parallel. In this case, I have configured the loop to run 3 threads which equates to 6 file uploads in parallel.
# ----- CONFIG -----$siteUrl = "https://tenantname.sharepoint.com/sites/yoursite"$library = "UPLOAD"$localFolder = "C:\temp\"$threads = 3$clientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"# ----- GET FILES -----$files = Get-ChildItem $localFolder -File# ----- PARALLEL UPLOAD -----$files | ForEach-Object -Parallel { # Reconnect inside each thread (required for PnP stability) Connect-PnPOnline -Url $using:siteUrl -Interactive -ClientId $using:clientId # Upload file $target = $using:library Add-PnPFile -Path $_.FullName -Folder $target Write-Host "Uploaded: $($_.Name)"} -ThrottleLimit $threads#Done
In this script I have a configuration section to set the paths and the ClientID from the PnP PowerShell App Registration used for authentication. The script gets all files in the specified folder and then loops through these files uploading to the document library configured at the top of the script.
There are some things you should consider when using this method.
- Microsoft 365 throttles if you do too many actions in parallel. You can add additional code to check for throttling. Increasing the number of threads can reduce performance if throttling occurs.
- If the order of uploads is important such as stacking versions of a document to create a version history, this won’t work.
- Memory and CPU constraints on the PC running the script can also limit performance.
- Add-PnPFile does multiple API calls to complete an action.
The overall performance is substantially better than using a sequential loop. If you need to bulk update files then this is definitely worth consideration.
Discover more from SharePoint Moments
Subscribe to get the latest posts sent to your email.
