Upload document using PowerShell Remotely with default account

upload document using powershell remotely sharepoint with default account

Today in this article I would like advance the script to upload the documents from user’s machine also called remote data upload without providing user credentials. Since we are not providing user credentials I am using “Invoke-WebRequest”. Using this script user can upload the documents to library but it will upload the document with “SYSTEM ACCOUNT” as user name.

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.

Solution:
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 script is to upload the documents from local folder to Sharepoint Document library without user stamp

function UploadDocuments($destination, $File)
{
try {
#Rading file data with IO stream
$content = [System.IO.File]::ReadAllBytes($File)
#appending file name with the destination document library path
$objFile = $destination+$File.Name
#This will upload the file in document library with “System Account” as user name
Invoke-WebRequest -Uri $objFile -Body $content -Method PUT -UseDefaultCredentials
}
catch {
Write-Host “Error:: $($_.Exception.Message)” -foregroundcolor red -BackgroundColor Yellow
}
}
# Set the variables
$destination = “<< Site URL >>”
$fileDirectory = “C:\Krishna\PowerShell Scripts\PS Testing\*.*”
#Reading through the document library
foreach($fileName in Get-ChildItem $fileDirectory)
{
#Calling method to upload the files from local folder
UploadDocuments -destination $destination -File $fileName
#Printing file name in Powershell command prompt
Write-Output “Uploaded File” $fileName
}
Write-Host “Script executed Successfully”
#Note: This script upload the files with System account. However who ever is executing the script must have
# contribute access on document library.

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

Since I have added comments for each and every line the code is self-explanatory.



How to execute this script:
Please follow the below steps to execute the PowerShell script:
1. Save the above script as “UploadDocuments_With out 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 out Credentials.ps1” as shown in the below screenshot.

upload document using powershell remotely sharepoint with default account
upload document using powershell remotely sharepoint with default account

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

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

powershell upload file to sharepoint online document library remotely with default account
powershell upload file to sharepoint online document library remotely with default account

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

Similar SharePoint 2013 Tutorials


About Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 12 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 →

Leave a Reply