In this blog, I will demonstrate how to upload files from a network path (a Directory) to a SharePoint Document Library folder using SharePoint PnP PowerShell Script.
Pre-requisites( Environment Details)
- Windows PowerShell
- SharePointPnpPowerShellOnline Module
Please install the SharePointPnpPowerShellOnline module if it’s not already present using the below command.
- Install-Module SharePointPnPPowerShellOnline
SharePoint Document Library Folder before copying files

Network File path from where Documents need to be copied to SharePoint Online

Variables Explanations in this Articles
- $O365ServiceAccount="[email protected]"# Your Service Account Name
- $O365ServiceAccountPwd="Test123$"#Your Service Account Password
- $SharePointSiteURL="https://abc.sharepoint.com/sites/Test" # Change this SharePoint Site URL
- $SharedDriveFolderPath=" C:\Users\kumavini\Documents\FolderA" # Change this Network Folder path
- $SharePointFolderPath="Shared Documents/FolderA" # Change the Document Library and Folder path
Here you can see we have provided the password in plain text which you need to provide if you want this PowerShell script to run automatically through a Timer Job or Scheduler.
For manual execution please use the Get-Credential command to read the user name and password from the user input.
Read Files from the Network Path(directory)
We have to read all files from the network path (directory) using the Get-ChildItem command of PowerShell as in the code snippet below.
- Get-ChildItem $SharedDriveFolderPath | Where-object {$_.PSIsContainer -ne $True}
The above code only reads files within the System Path directory, not any folder within the directory.
We can apply a sorting feature too if we would like to read the files in ascending or descending order based on any file properties. Below is the snippet to read all files within a directory with descending order of creation time.
- Get-ChildItem $SharedDriveFolderPath | Where-object {$_.PSIsContainer -ne $True} | Sort-Object CreationTime –Descending
Now apply For Each to read all the files one by one in the below snippet.
- Get-ChildItem $SharedDriveFolderPath | Where-object {$_.PSIsContainer -ne $True} | Sort-Object CreationTime -Descending | ForEach-Object {
- }
Copy files to SharePoint Online Document Library
SharePointPnpPowerShellOnline Module of PowerShell has made developers' lives easier. To add a file to a SharePoint Document Library Path use the code snippet as below.
Please find all the parameters associated with Add-PnPFile
- Add-PnPFile -Path $_.FullName -folder $SharePointFolderPath #Add File to SharePoint Document Library folder
$_.FullName is full path name of the file with extension when we loop to all File Objects.
Below is the example of a hard coded full file path name to be uploaded in SharePoint Online Document Library.
- Add-PnPFile -Path “C:\Users\kumavini\Documents\FolderA\ testdoc.xlsx” -folder $SharePointFolderPath
Before copying the files to SharePoint online you should be connecting to the SharePoint Site using the below snippet.
- [SecureString]$SecurePass = ConvertTo-SecureString $O365ServiceAccountPwd -AsPlainText -Force
- [System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($O365ServiceAccount, $SecurePass)
- #Connecting to SharePoint Online site
- Connect-PnPOnline -Url $SharePointSiteURL -Credentials $PSCredentials
Complete PowerShell Script
- #Powershell script to copy files from Shared drive to SharePoint Online
- #Created By: Vinit Kumar
- # Variable - Change the parameter as it need
- $O365ServiceAccount="[email protected]"# Your User name
- $O365ServiceAccountPwd="Test123$"#Your Password - You can use Get-credentials command as well
- $SharePointSiteURL="https://abc.sharepoint.com/sites/Test" # Change this SharePOint Site URL
- $SharedDriveFolderPath="C:\Users\kumavini\Documents\FolderA" # Change this Network Folder path
- $SharePointFolderPath="Shared Documents/FolderA" # Change the Document Library and Folder path
- #Ends
- [SecureString]$SecurePass = ConvertTo-SecureString $O365ServiceAccountPwd -AsPlainText -Force
- [System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($O365ServiceAccount, $SecurePass)
- #Connecting to SharePoint Online site
- Connect-PnPOnline -Url $SharePointSiteURL -Credentials $PSCredentials
- Get-ChildItem $SharedDriveFolderPath | Where-object {$_.PSIsContainer -ne $True} | Sort-Object CreationTime -Descending | ForEach-Object {
- Add-PnPFile -Path $_.FullName -folder $SharePointFolderPath #Add File to SharePoint Document Library folder
- }
SharePoint Online Document Library Folder Output

In this blog, we have talked about how to copy all files from a Network shared drive directory to SharePoint Online Document Library Folder using SharePoint Online PnP PowerShell script. In my next blog will write about how to download all files from a SharePoint Online Document Library Folder to a network drive using SharePoint Online PnP PowerShell Script.

sowmya sreePosted Dec 2, 2022, 5:38 PM
Hi is there any way to download sharepoint online document library files and folders to multiple servers network path at a time.
lslhome DevicesPosted Mar 20, 2021, 11:20 PM
What i really want to do is to upload from Network share to SharePoint 0365 document Libaray: # Variable - Change the parameter as it need $O365ServiceAccount="[email protected]"# Your User name $O365ServiceAccountPwd="Opend00r"#Your Password - You can use Get-credentials command as well $SharePointSiteURL="https://ayonrinde.sharepoint.com/sites/powershell" # Change this SharePOint Site URL $SharedDriveFolderPath="\\lakes-systems\global\IT" # Change this Network Folder path $SharePointFolderPath="Shared Documents" # Change the Document Library and Folder path #Ends [SecureString]$SecurePass = ConvertTo-SecureString $O365ServiceAccountPwd -AsPlainText -Force [System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($O365ServiceAccount, $SecurePass) #Connecting to SharePoint Online site Connect-PnPOnline -Url $SharePointSiteURL -Credentials $PSCredentials Get-ChildItem $SharedDriveFolderPath | Where-object {$_.PSIsContainer -ne $True} | Sort-Object CreationTime -Descending | ForEach-Object { Add-PnPFile -Path $_.FullName -folder $"Shared Documents/IT" #Add File to SharePoint Document Library folder }
lslhome DevicesPosted Mar 20, 2021, 11:07 PM
However, i got errors when i copy and modified :PS C:\Windows\system32> C:\dfsreport\upload2sharepoint.ps1Connect-PnPOnline : The remote name could not be resolved: 'ayonrinde.sharepoint.com' At C:\dfsreport\upload2sharepoint.ps1:15 char:5 + Connect-PnPOnline -Url $SharePointSiteURL -Credentials $PSCredent ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Connect-PnPOnline], WebException + FullyQualifiedErrorId : System.Net.WebException,PnP.PowerShell.Commands.Base.ConnectOnline Add-PnPFile : The remote name could not be resolved: 'ayonrinde.sharepoint.com' At C:\dfsreport\upload2sharepoint.ps1:19 char:8 + Add-PnPFile -Path $_.FullName -folder $"Shared Documents" #Add ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Add-PnPFile], WebException + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.AddFile Add-PnPFile : The remote name could not be resolved: 'ayonrinde.sharepoint.com' At C:\dfsreport\upload2sharepoint.ps1:19 char:8 + Add-PnPFile -Path $_.FullName -folder $"Shared Documents" #Add ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Add-PnPFile], WebException + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.AddFile Add-PnPFile : The remote name could not be resolved: 'ayonrinde.sharepoint.com' At C:\dfsreport\upload2sharepoint.ps1:19 char:8 + Add-PnPFile -Path $_.FullName -folder $"Shared Documents" #Add ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Add-PnPFile], WebException + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.AddFile Add-PnPFile : The remote name could not be resolved: 'ayonrinde.sharepoint.com' At C:\dfsreport\upload2sharepoint.ps1:19 char:8 + Add-PnPFile -Path $_.FullName -folder $"Shared Documents" #Add ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Add-PnPFile], WebException + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.AddFile Add-PnPFile : The remote name could not be resolved: 'ayonrinde.sharepoint.com' At C:\dfsreport\upload2sharepoint.ps1:19 char:8 + Add-PnPFile -Path $_.FullName -folder $"Shared Documents" #Add ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Add-PnPFile], WebException + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.AddFile Add-PnPFile : The remote name could not be resolved: 'ayonrinde.sharepoint.com' At C:\dfsreport\upload2sharepoint.ps1:19 char:8 + Add-PnPFile -Path $_.FullName -folder $"Shared Documents" #Add ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Add-PnPFile], WebException + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.AddFile Add-PnPFile : The remote name could not be resolved: 'ayonrinde.sharepoint.com' At C:\dfsreport\upload2sharepoint.ps1:19 char:8 + Add-PnPFile -Path $_.FullName -folder $"Shared Documents" #Add ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Add-PnPFile], WebException + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.AddFile Add-PnPFile : The remote name could not be resolved: 'ayonrinde.sharepoint.com' At C:\dfsreport\upload2sharepoint.ps1:19 char:8 + Add-PnPFile -Path $_.FullName -folder $"Shared Documents" #Add ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Add-PnPFile], WebException + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.AddFile Add-PnPFile : The remote name could not be resolved: 'ayonrinde.sharepoint.com' At C:\dfsreport\upload2sharepoint.ps1:19 char:8 + Add-PnPFile -Path $_.FullName -folder $"Shared Documents" #Add ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Add-PnPFile], WebException + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.AddFile Add-PnPFile : The remote name could not be resolved: 'ayonrinde.sharepoint.com' At C:\dfsreport\upload2sharepoint.ps1:19 char:8 + Add-PnPFile -Path $_.FullName -folder $"Shared Documents" #Add ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Add-PnPFile], WebException + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.AddFile Add-PnPFile : The remote name could not be resolved: 'ayonrinde.sharepoint.com' At C:\dfsreport\upload2sharepoint.ps1:19 char:8 + Add-PnPFile -Path $_.FullName -folder $"Shared Documents" #Add ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Add-PnPFile], WebException + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.AddFile Add-PnPFile : The remote name could not be resolved: 'ayonrinde.sharepoint.com' At C:\dfsreport\upload2sharepoint.ps1:19 char:8 + Add-PnPFile -Path $_.FullName -folder $"Shared Documents" #Add ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Add-PnPFile], WebException + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.AddFile Add-PnPFile : The remote name could not be resolved: 'ayonrinde.sharepoint.com' At C:\dfsreport\upload2sharepoint.ps1:19 char:8 + Add-PnPFile -Path $_.FullName -folder $"Shared Documents" #Add ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Add-PnPFile], WebException + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.AddFile Add-PnPFile : The remote name could not be resolved: 'ayonrinde.sharepoint.com' At C:\dfsreport\upload2sharepoint.ps1:19 char:8 + Add-PnPFile -Path $_.FullName -folder $"Shared Documents" #Add ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Add-PnPFile], WebException + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.AddFile PS C:\Windows\system32>
lslhome DevicesPosted Mar 20, 2021, 11:05 PM
Thank you ever so much for sharing this have been looking for a way to script this type of task.
Moler FrankoPosted Mar 18, 2021, 7:23 PM
Great Ideas here in your article, I was using some in the past, now I use Gs Richcopy 360 to copy directly from our server to SharePoint Online with the option of throttling the connection to save the bandwidth during the daily operation
Vinay AyinapurapuPosted Feb 4, 2021, 1:37 AM
What happens if the files already exist? will it overwrite the existing file?