Upload Files From Network Drive (System File Path) To SharePoint Online Document Library Folder Using SharePoint PnP PowerShell

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.
  1. Install-Module SharePointPnPPowerShellOnline  
SharePoint Document Library Folder before copying files
 
Copy Files From Network Drive (System File Path) To SharePoint Online Document Library Folder Using SharePoint PnP PowerShell
 
Network File path from where Documents need to be copied to SharePoint Online
 
Copy Files From Network Drive (System File Path) To SharePoint Online Document Library Folder Using SharePoint PnP PowerShell
 
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.
  1. 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.
  1. 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.
  1. Get-ChildItem $SharedDriveFolderPath | Where-object {$_.PSIsContainer -ne $True} | Sort-Object CreationTime -Descending | ForEach-Object {  
  2. }  
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
  1. 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.
  1. 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.
  1. [SecureString]$SecurePass = ConvertTo-SecureString $O365ServiceAccountPwd -AsPlainText -Force  
  2. [System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($O365ServiceAccount, $SecurePass)  
  3. #Connecting to SharePoint Online site  
  4. Connect-PnPOnline -Url $SharePointSiteURL -Credentials $PSCredentials  
Complete PowerShell Script
  1. #Powershell script to copy files from Shared drive to SharePoint Online  
  2. #Created By: Vinit Kumar  
  3.   
  4. # Variable - Change the parameter as it need  
  5. $O365ServiceAccount="[email protected]"# Your User name  
  6. $O365ServiceAccountPwd="Test123$"#Your Password - You can use Get-credentials command as well  
  7. $SharePointSiteURL="https://abc.sharepoint.com/sites/Test" # Change this SharePOint Site URL  
  8. $SharedDriveFolderPath="C:\Users\kumavini\Documents\FolderA" # Change this Network Folder path  
  9. $SharePointFolderPath="Shared Documents/FolderA" # Change the Document Library and Folder path  
  10. #Ends  
  11.   
  12. [SecureString]$SecurePass = ConvertTo-SecureString $O365ServiceAccountPwd -AsPlainText -Force  
  13. [System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($O365ServiceAccount, $SecurePass)  
  14. #Connecting to SharePoint Online site  
  15. Connect-PnPOnline -Url $SharePointSiteURL -Credentials $PSCredentials  
  16.   
  17.   
  18. Get-ChildItem $SharedDriveFolderPath | Where-object {$_.PSIsContainer -ne $True} | Sort-Object CreationTime -Descending | ForEach-Object {  
  19.    Add-PnPFile -Path $_.FullName -folder $SharePointFolderPath #Add File to SharePoint Document Library folder  
  20. }  
SharePoint Online Document Library Folder Output
 
Copy Files From Network Drive (System File Path) To SharePoint Online Document Library Folder Using SharePoint PnP PowerShell
 
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.