SharePoint PnP PowerShell - How To Upload Files In SharePoint Online Using PowerShell

Introduction

SharePointPnP.PowerShell solution contains a library of PowerShell commands, which allows you to perform complex provisioning and artifact management actions towards SharePoint. The commands use CSOM and can work against both SharePoint Online as SharePoint On-Premises.

Prerequisites

Install SharePointPnPPowerShellOnline.msi for SharePoint Online.

Upload files

Open Notepad and paste XML given below. Save the text file as Inputs.xml.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Inputs>  
  3.   <ConnectSPOnline SiteURL="https://c986.sharepoint.com/sites/vijai" UserName="[email protected]" Password="xxxxxxxxx"></ConnectSPOnline>  
  4.  <UploadFiles>  
  5.     <UploadFile SourceFolder=".\HTML" DestinationFolder="SiteAssets" CreateFolder="yes" FolderName="HTML"></UploadFile>  
  6.     <UploadFile SourceFolder=".\CSS" DestinationFolder="SiteAssets" CreateFolder="yes" FolderName="CSS"></UploadFile>  
  7.     <UploadFile SourceFolder=".\Files" DestinationFolder="SiteAssets" CreateFolder="no" FolderName=""></UploadFile>  
  8.   </UploadFiles>  
  9. </Inputs>  

Open Notepad and paste the script given below. Save the text file as uploadfiles.ps1.

  1. ############################################################## Logging #########################################
  2.   
  3. $date= Get-Date -format MMddyyyyHHmmss  
  4. start-transcript -path .\Log_$date.doc   
  5.  
  6. ################################################### Get input parameters from XML ###############################
  7.  
  8. # Get content from XML file  
  9. [xml]$xmlData=Get-Content ".\Inputs.xml"  
  10.  
  11. # ConnectSPOnline node  
  12. [System.Xml.XmlElement]$connectSPOnline = $xmlData.Inputs.ConnectSPOnline  
  13. $siteURL=$connectSPOnline.SiteURL  
  14. $userName=$connectSPOnline.UserName  
  15. $password=$connectSPOnline.Password  
  16.  
  17. # UploadFiles node  
  18. [System.Xml.XmlElement]$uploadFiles = $xmlData.Inputs.UploadFiles  
  19.  
  20. ########################################################## Get Credentials ######################################
  21.   
  22. function GetCredentials()  
  23. {   
  24.     write-host -ForegroundColor Green "Get Credentials and connect to SP Online site: " $siteURL  
  25.     # Convert password to secure string    
  26.     $secureStringPwd = ConvertTo-SecureString -AsPlainText $Password -Force  
  27.  
  28.     # Get the credentials  
  29.     $credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName,$secureStringPwd   
  30.  
  31.     # Connect to SP online site  
  32.     Connect-PnPOnline –Url $siteURL –Credentials $credentials      
  33. }  
  34.  
  35. ########################################################### Upload Files ########################################
  36.   
  37. function UploadFiles()  
  38. {  
  39.     write-host -ForegroundColor Green "Upload files to SP Online site"  
  40.  
  41.     # Loop through UploadFiles XML node  
  42.     foreach($uploadFile in $uploadFiles.UploadFile)  
  43.     {  
  44.         # UploadFile node parameters  
  45.         $ufSourceFolder=$uploadFile.SourceFolder  
  46.         $upDestinationFolder=$uploadFile.DestinationFolder  
  47.         $ufCreateFolder=$uploadFile.CreateFolder  
  48.         $ufFolderName=$uploadFile.FolderName          
  49.          
  50.         # Check whether folder has to be created  
  51.         if($ufCreateFolder="yes")  
  52.         {     
  53.             $siteRelativePath=$upDestinationFolder+"/"+$ufFolderName  
  54.              
  55.             # Returns a folder from a given site relative path, and will create it if it does not exist.             
  56.             Ensure-PnPFolder -SiteRelativePath $siteRelativePath               
  57.         }   
  58.         else  
  59.         {  
  60.             $siteRelativePath=$upDestinationFolder              
  61.         }  
  62.   
  63.         write-host -ForegroundColor Yellow "Uploading files - Source Folder: " $ufSourceFolder " - Destination Folder: " $siteRelativePath  
  64.  
  65.         # Loop through all the files  
  66.         foreach($file in Get-ChildItem $ufSourceFolder)  
  67.         {  
  68.             $filePath=$ufSourceFolder+"\"+$file.Name    
  69.             write-host -ForegroundColor Magenta "uploading the file.... " $file.Name  
  70.  
  71.             # Add files to the respective folder  
  72.             Add-PnPFile -Path $filePath -Folder $siteRelativePath  
  73.         }   
  74.     }  
  75. }  
  76.  
  77. #################################################################  Initiate #####################################
  78.   
  79. function Initiate()  
  80. {  
  81.      write-host -ForegroundColor Green "Initiating the script.................. "   
  82.  
  83.      # Get Credentials and connect to SP Online site  
  84.      GetCredentials  
  85.  
  86.      # Call the required functions  
  87.      UploadFiles  
  88.  
  89.      # Disconnect from the server  
  90.      Disconnect-PnPOnline  
  91.   
  92.      write-host -ForegroundColor Green "Completed!!!!"   
  93. }  
  94.  
  95. #################################################################################################################
  96.   
  97. Initiate  
  98. Stop-Transcript  

Create folders and add the files to the folder. See the folder structure for the reference.

 

Run Windows PowerShell as an administrator.

Navigate to the folder, where XML and ps1 files are available.

Type .\uploadfiles.ps1 and click Enter.



Result

The folders are created, which are based on the input provided in an XML file and the files are uploaded to the respective folder. In this blog, you saw how to upload the files in SharePoint Online, using PowerShell.

 

Reference

  • https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/EnsurePnPFolder.md
  • https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/AddPnPFile.md