Upload document using PowerShell Remotely

powershell upload file to sharepoint online document library remotely
SharePoint deveopment training course

Today in this article I would like advance the script to upload the documents from user’s machine also called remote data upload. Since we can’t get the object of SharePoint site in a PowerShell script I am using the WebClient object. WebClient is one of the way to get the object for a site.

The requirement is still remain same as last time that user want to migrate all the documents from his network drive to SharePoint document library as part data migration. Since there are millions of files in user network drive, I can’t ask the user to upload all his files one by one nor with the explorer view. Copying files manually will take quite a good time of time.

The below PowerShell script uploads all the user documents to document library. This script I can run request end user to execute on his local machine as and when he / she require. Another way is that I can run on behalf of business user on my machine without doing a remote to SharePoint server.

The power shell script is as follows:

# This Powershell script is to uplaod the files to a sharepoint document library REMOTELY with user Credentails
function UploadDocuments($destination, $File,$userID, $securePasssword)
try {
# Since we’re doing this remotely, we need to authenticate
$credentials = New-Object System.Management.Automation.PSCredential ($userID, $securePasssword)

# Upload the file
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = $credentials
$webclient.UploadFile($destination + “/” + $File.Name, “PUT”, $File.FullName)
catch {
Write-Host “Error:: $($_.Exception.Message)” -foregroundcolor red -BackgroundColor Yellow

# Set the variables
$destination = “<<Document Library URL >>”
$fileDirectory = “C:\Krishna\PowerShell Scripts\PS Testing\T\*.*”
$userName = Read-Host “Enter User-ID (domain\userID):: ”
$securePasssword = Read-Host “Please enter the password for user $($userName) :: ” -AsSecureString
#Reading through the folder
foreach($fileName in Get-ChildItem $fileDirectory)
UploadDocuments -destination $destination -File $fileName -userID $userName -securePasssword $securePasssword
Write-Host “Uploaded File=” $fileName.Name
Write-Host “Script executed Successfully”

Whoever is running script should have contribute access on the document library 

Let us walk through the script:

I am reading password with the below line of code in PowerShell command prompt:
$securePasssword = Read-Host “Please enter the password:” -AsSecureString

The below line is to associate the user name and password which will be used while creating object to SharePoint site.
$credentials = New-Object System.Management.Automation.PSCredential (“<<Domain\userID>”, $securePasssword)

We are making call to get the object to SharePoint using provided user credentials and WebClient.
# Upload the file
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = $credentials
$webclient.UploadFile($destination + “/” + $File.Name, “PUT”, $File.FullName)

Uploading the file to document library:
$webclient.UploadFile($destination + “/” + $File.Name, “PUT”, $File.FullName)
– In the above line destination is name of the document library
– $File.Name is the name of the file to be created as in document library
– $File.FullName is the path of the file from where it needs to be uploaded from

How to execute this script:
Please follow the below steps to execute the PowerShell script:
1. Save the above script as “UploadDocuments_With Credentials.ps1”
2. Open PowerShell Command let as administrator
3. Navigate to the folder where you save the PS1 script
4. Execute the script by typing “.\UploadDocuments_With Credentials.ps1” as shown in the below screenshot.

upload document using powershell remotely sharepoint
upload document using powershell remotely sharepoint

5. This script has will uploaded all the file in “C:\Temp\PS Testing\T” folder as mentioned in the code.

Files uploaded to SharePoint Library:
In the output you can see the uploaded user name.

powershell upload file to sharepoint online document library remotely
powershell upload file to sharepoint online document library remotely

Note: This script will not work for SharePoint online sites.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)


About Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 13+ years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019

View all posts by Krishna Vandanapu →