Powershell: Copy local file to SharePoint

Huff Jennifer 275 Reputation points
2025-07-04T11:56:52.2666667+00:00

How to use PWS for copy local files to SharePoint?

Microsoft 365 and Office | SharePoint | Development

Locked Question. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
Steven-N 25,315 Reputation points Microsoft External Staff Moderator
2025-07-04T13:23:04.4+00:00

Hi Huff Jennifer  

Thanks for reaching out to Microsoft Q&A forum support 

Regarding to your question, there are 2 options to use Powershell to copy the local file to SharePoint 

Option 1: Window Powershell, you can try to use this type script

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Variables for Processing
$WebUrl = "https://crescent.sharepoint.com/Sites/Sales"
$LibraryName ="Documents"
$SourceFile="C:\SitesToCreate.csv"
$AdminName ="******@crescent.com"
$AdminPassword ="password goes here"
  
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminName,(ConvertTo-SecureString $AdminPassword -AsPlainText -Force))
  
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl) 
$Context.Credentials = $Credentials
 
#Get the Library
$Library =  $Context.Web.Lists.GetByTitle($LibraryName)
 
#Get the file from disk
$FileStream = ([System.IO.FileInfo] (Get-Item $SourceFile)).OpenRead()
#Get File Name from source file path
$SourceFileName = Split-path $SourceFile -leaf
   
#sharepoint online upload file powershell
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $SourceFileName
$FileUploaded = $Library.RootFolder.Files.Add($FileCreationInfo)
  
#powershell upload single file to sharepoint online
$Context.Load($FileUploaded) 
$Context.ExecuteQuery() 
 
#Close file stream
$FileStream.Close()
  
write-host "File has been uploaded!"

For bulk file uploading

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
### Variables for Processing
$WebUrl = "https://crescent.sharepoint.com/Sites/Sales"
$LibraryName ="Documents"
$SourceFolder="C:\Documents"
$UserName ="******@crescent.com"
$Password ="password goes here"
  
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
  
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl) 
$Context.Credentials = $Credentials
 
#Get the Library
$Library =  $Context.Web.Lists.GetByTitle($LibraryName)
 
#upload each file from the directory
Foreach ($File in  (dir $SourceFolder -File))
{
    #Get the file from disk
    $FileStream = ([System.IO.FileInfo] (Get-Item $File.FullName)).OpenRead()
   
    #Upload the File to SharePoint Library
    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $FileCreationInfo.Overwrite = $true
    $FileCreationInfo.ContentStream = $FileStream
    $FileCreationInfo.URL = $File
    $FileUploaded = $Library.RootFolder.Files.Add($FileCreationInfo)
  
    #powershell to upload files to sharepoint online
    $Context.Load($FileUploaded) 
    $Context.ExecuteQuery() 
 
    #Close file stream
    $FileStream.Close()
 
    write-host "File: $($File) has been uploaded!"
} 

Option 2: PnP Powershell 

#Config Variables
$SiteURL = "https://xxx.sharepoint.com"
$SourceFilePath ="C:\Documents\xxx.docx"
$DestinationPath = "Shared Documents" #Site Relative Path of the Library
 
  
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -interactive
      
#powershell pnp to upload file to sharepoint online
Add-PnPFile -Path $SourceFilePath -Folder $DestinationPath

For more information, you can check at: Add-PnPFile | PnP PowerShell

We hope this information is helpful and appreciate your understanding.  

If you need further assistance, please let us know.  


If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".    

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.  

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more