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.
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)
#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
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.
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.
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.
Note: This script will not work for SharePoint online sites.
-: FREE PDF e-Book Download :-
51 JSOM Examples in SharePoint Online
You May Also like the Following SharePoint Online Tutorials: