Upload Files to SharePoint Remotely using Web Client and PowerShell

If you want to upload file to SharePoint document library from client side (or remotely), Here is the nifty PowerShell code snippet.

Upload File to SharePoint using Web Client PowerShell:

#Variable for Source folder in Local machine & Destination in SharePoint
$Source ="c:\temp\file.pdf"
$Target="https://portal.crescent.com/projects/Documents/File.pdf"
 
#Create new web client object
$WebClient = new-object System.Net.WebClient
$Webclient.UseDefaultCredentials = $true

#upload file to sharepoint using webclient
$WebClient.UploadFile($Target,"PUT", $Source) 

Upload All Files in a Folder to SharePoint – Web Client HTTP put method:

#Variable for Source folder in Local machine
$FilesLocation ="D:\Scripts\Reports\"
$TargetLocation="https://Operations.Crescent.com/Reports/"

#Create new web client object
$WebClient = new-object System.Net.WebClient
$WebClient.Credentials = [System.Net.CredentialCache]::DefaultCredentials

#Use these lines if you use different credentials
#$cred = new-object System.Net.NetworkCredential("USER NAME","PASSWORD","DOMAIN")
#$WebClient.credentials = $cred

#Target File Location to upload
function Set-DestinationFile($FileName)
{ 
   $TargetLocation + $(split-path -leaf $FileName)
}

#Iterate through each file and upload
Get-ChildItem $FilesLocation | ForEach-Object {

  $DestinationFile= Set-DestinationFile $_;

  #upload file to sharepoint using webclient
  $WebClient.UploadFile($DestinationFile,"PUT", $_.FullName)
} 

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *