How to upload files to SharePoint using PowerShell

In this PowerShell SharePoint tutorial, we will discuss how to upload files to SharePoint using PowerShell.

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

The requirement still remains the same as last time that the user wants to migrate all the documents from his network drive to the SharePoint document library as part of data migration. Since there are millions of files in the 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 the business user on my machine without doing a remote to the SharePoint server.

Upload files to sharepoint using PowerShell Remotely

Let us see, how to upload files to SharePoint using PowerShell remotely.

The PowerShell 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"

Note:
Whoever is running script should have contribute access to the SharePoint 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 the 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 SharePoint document library:

$webclient.UploadFile($destination + "/" + $File.Name, "PUT", $File.FullName)

In the above line destination is the 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 run this PowerShell script?

Please follow the below steps to execute the PowerShell script:

  • Save the above script as “UploadDocuments_With Credentials.ps1”
  • Open PowerShell Command let as administrator
  • Navigate to the folder where you save the PS1 script
  • Execute the script by typing “.\UploadDocuments_With Credentials.ps1” as shown in the below screenshot.
Upload documents to SharePoint document library using PowerShell Remotely
upload files to sharepoint using powershell
  • This script has will upload 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.

Upload documents to SharePoint document library using PowerShell Remotely
How to upload files to sharepoint using powershell

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

Read: PowerShell SharePoint list operations and PowerShell SharePoint site collection example.

How to upload files to SharePoint using PowerShell Remotely with default account

Here we will see how to upload documents to SharePoint Document Library using PowerShell Remotely with the default account.

Now, I would like to 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 the library but it will upload the document with “SYSTEM ACCOUNT” as user name.

The requirement still remains the same as last time that the user wants to migrate all the documents from his network drive to the SharePoint document library as part of data migration. Since there are millions of files in the 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.

See also  SharePoint Online Microsoft Forms Web part

The below PowerShell script uploads all the user documents to the 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 the business user on my machine without doing a remote to the SharePoint server.

The PowerShell 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 contributed access to the document library.

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

How to execute this PowerShell script:

Please follow the below steps to execute the PowerShell script:

  • Save the above script as “UploadDocuments_With out Credentials.ps1”
  • Open PowerShell Command let as administrator
  • Navigate to the folder where you save the PS1 script
  • Execute the script by typing “.\UploadDocuments_With out Credentials.ps1” as shown in the below screenshot.
upload files to SharePoint using PowerShell
upload file to sharepoint online programmatically
  • This script has will upload 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.

upload files to sharepoint online using powershell
upload file to sharepoint online programmatically

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

Read: 40 Useful PowerShell SharePoint Commands.

Upload files to SharePoint online using PowerShell

Let us see, how to upload files to sharepoint online using powershell.

I would like to speak about the process to upload the documents to the SharePoint Online site from your local environment.

However, the requirement still remains the same as last time that user wants to migrate all the documents from his network / local drive to SharePoint online document library as part data migration.

Since there are millions of files in the user’s 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 SharePoint Online 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 the business user on my machine seamlessly.

Below is the PowerShell script to upload documents to the SharePoint Online document library remotely.

try {
#Specify tenant admin and site URL and user name
$User = "[email protected]"
$SiteURL = https://onlysharepoint2013.sharepoint.com/sites/krishna
#Specify Local folder name where the files are to be uploaded
$Folder = "G:\KVN Articles\PS Testing\Files"
$DocLibName = "Finance"

$startedTime = Get-Date
Write-Host "Time Started = " $startedTime

#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client.Runtime\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.Runtime.dll"
#Reading Password from PowerShell Command line
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

#Creating Object to Document Library
$List = $Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List)
$Context.ExecuteQuery()
#Reading through the local folder for files
Foreach ($File in (dir $Folder -File))
{
#Reading File data using IO stream object
$FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $File
#Uploading file to document library
$Upload = $List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
Write-Host "Uploaded File=" $File.Name
}
$completedTime = Get-Date
Write-Host "Time Completed = " $completedTime
Write-Host "Script Excecuted Successfully !!!"
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor red
}

I strongly believe that the script is self-explanatory as I have added comments for most of the lines.

Note:
To execute this script user must install the SharePoint client components SDK on his/her machine.

Steps to be followed to ensure the smooth execution:

Ensure that SharePoint Client components SDK installed on the user machine

Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll paths mentioned in the script are the same as the user’s machine

Open PowerShell command-let with Run as Administrator

Check the “execution policies” for PowerShell in user machine by executing below command in PowerShell command let

Get-ExecutionPolicy -List

The above command will get all the existing policies of the user mostly the output will be as follows:

ScopeExecutionPolicy
MachinePolicyUndefined
UserPolicy Undefined
Process Undefined
CurrentUser Undefined
LocalMachine  Undefined

We need to change the execution policy from Undefined to RemotelySigned with the below command

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

Ensure that the policy got updated properly by executing the Get command as part of step-4

See also  How to Send Email to SharePoint Group in Power Automate?

Now execute the PowerShell script as “.\<> by navigating on the respective folder location of the script

Once the files are uploaded successfully, please revert the execution policies back to its original state by executing the below line

Set-ExecutionPolicy Undefined -Scope LocalMachine

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

upload file to sharepoint library using powershell
upload file to sharepoint library using powershell

This is how upload files to sharepoint online using powershell.

Read: How to Change SharePoint Site Logo + PowerShell

Upload files from network drive to SharePoint using PowerShell

Now, let us see how to upload documents from network drive to SharePoint using PowerShell.

The user wants to migrate all the documents from his network drive to the SharePoint document library as part of data migration. Since there are millions of files in the 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.

I have written a PowerShell script to upload all the user documents to the document library. This script I can schedule on SharePoint server and it can run on its own without user intervention.

The PowerShell script is as follows:

function UploadDocuments ($WebURL,$DocLibName,$FilePath) {
try {

# Get a variable that points to the folder
$Web = Get-SPWeb $WebURL
$List = $Web.GetFolder($DocLibName)
$Files = $List.Files

# Get just the name of the file from the whole path
$FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1)

# Load the file into a variable
$File= Get-ChildItem $FilePath

# Upload it to SharePoint
$Files.Add($DocLibName +"/" + $FileName,$File.OpenRead(),$true)
$web.Dispose()
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor red -BackgroundColor Yellow
}
}
# Set the variables
$WebURL = "<< Site URL >>"
$DocLibName = "Docs"
$fileDirectory = "C:\Temp\PS Testing\T\*.*"
foreach($fileName in Get-ChildItem $fileDirectory)
{
#Reading file by file
UploadDocuments -WebURL $WebURL -DocLibName $DocLibName -FilePath $fileName.FullName
Write-Host "Uploaded File=" $fileName.Name
}
write-host "Script executed successfully !!!" -foregroundcolor White -BackgroundColor Green

How to execute this script:

Please follow the below steps to execute the PowerShell script:

  • Log on to Application Server (where you can see Central admin)
  • Open SharePoint 2013 management shell as administrator
  • Copy/Save above script as “UploadDocuments_onSharePointServer.ps1” (You can change the name if you want there is no dependency of the file name) in c:\
  • Navigate on to c:\ in the Management shell
  • Execute the script by typing “.\<<FileName>>”
sharepoint upload file to document library powershell
sharepoint upload file to document library powershell
  • This will upload all the file in “C:\Temp\PS Testing\T” folder

Files in folder “C:\Temp\PS Testing\T”

upload documents from network drive to SharePoint using powershell

Output in Management Shell:

upload file to sharepoint document library using powershell
How to upload files to SharePoint using PowerShell

Files uploaded to SharePoint Library:

upload file to sharepoint document library powershell
upload files to sharepoint using powershell

Let us see, how to upload documents from local drive to SharePoint 2016/2013 document library using PowerShell.

Unable to upload large files to SharePoint using PowerShell

I am sure most of us might have experienced this issue “can’t upload large files to SharePoint”. By default, the maximum size for uploading files is set to 250 MB. The maximum file size that it can go up to is 2,047 megabytes. If the file size is greater than 2 GB you will see one of the below errors:

  • An unexpected error has occurred
  • The page cannot be displayed
  • An unknown error occurred
  • HTTP 404 – Page Not Found
  • Request timed Out

In fact sometimes the user might see one these error even if the file size is only 500 MB. This can occur due to various reasons now I would like to discuss a few cases with the solutions for each.

Note: File size parameter/property is at the web application level, as per Microsoft best practices it is strongly recommended not to increase the size because it will impact the performance of that web application.

Case-I: Maximum upload size for web application is less than the file size:

As discussed just before the default file size of a web application is 250 MB. Increase the file size for the required web application by following the below steps.

Log on to application server with the FARM administrator account.

Click on Manage web application under Web applications in SharePoint Central Admin.

Central Administration -> Application Management -> Manage web application.

Request timed Out sharepoint
can not upload large file to sharepoint

It will list all the Web Applications in the FARM

Select the web application in which you want to increase the file size and click on General setting from ribbon control.

unable to upload large file to sharepoint 2013
unable to upload large file to sharepoint 2013

It will show all the General Settings of the web Application like Default Quota Template, Alerts, RSS settings, Recycle Bin settings of web application and Maximum upload size limit

You should be able to see as below by default.

unable to upload large file to sharepoint
unable to upload large file to sharepoint

Now change it to 2047 MB to support files up to 2GB and click ok

Case II: Increase the connection time-out settings in IIS:

While uploading large files, there are chances that the request will timeout. By default, the IIS connection time-out setting is 120 seconds. Follow these steps to increase the connection time-out setting,

See also  [Solved] Microsoft.SharePoint.Client.IdcrlException: The sign-in name or password does not match one in the Microsoft account system error in SharePoint Online

Click Start, point to All Programs, point to Administrative Tools, and then click Internet Information Services (IIS) Manager.

Right-click the virtual server that you want to configure, and then click Properties.

Click the Web Site tab. Under Connections, type the number of seconds that you want in the Connection time-out box, and then click OK.

upload large file to sharepoint 2016
upload large file to sharepoint 2016
upload large file to sharepoint 2013
upload large file to sharepoint 2013

Case III: Increase the default chunk size for large files:

The large-file-chunk-size property sets the amount of data that can be read from server running SQL Server at one time.

If you have a file that is greater than your chunk size (such as 70 MB when the chunk size is set to 5 MB), the file would be read in 14 chunks (70 / 5).

The chunk size is not related to the maximum upload file size.

The chunk size simply specifies the amount of data that can be read from a file at one time. By default, the large-file-chunk-size property is set to 5 MB.

Check if the ‘large-file-chunk-size’ property is set or not

Stsadm -o getproperty -propertyname large-file-chunk-size

In order to set the large–file–chunk–size property, we need to use the command line. This property is configured for a server or server farm, and cannot be configured for an individual web app server. To set this property, use the following syntax:

Stsadm.exe –o setproperty –pn large–file–chunk–size –pv

More on this command is available at Large-file-chunk-size: Stsadm property (Office SharePoint Server)

After making a change to this property, perform an iisreset/noforce.

This is how, we can fix a few errors while uploading large files to SharePoint like, unable to upload large file to sharepoint, can not upload large file to sharepoint, an unexpected error has occurred in sharepoint 2013, the page cannot be displayed in sharepoint 2013, the webpage cannot be found http 404 sharepoint 2013, request timed out sharepoint 2013, etc.

Migrate SharePoint document library with folder structure

Let us see, how to migrate files with the folder structure to the SharePoint 2013 document library using PowerShell. As part of a migration project, we get a request from an end-user to migrate their data from various places.

Here, I would like to take one of the scenarios to upload files from the local drive/network share with the same folder structure into SharePoint.

Local Folder structure:

migrate sharepoint document library
migrate sharepoint document library

PowerShell Script to migrate document with folder structure in SharePoint 2013

Below is the PowerShell script to migrate document with folder structure in SharePoint 2013.

try {
#Referencing to SharePoint PowerShell
Add-PsSnapin Microsoft.SharePoint.PowerShell -erroraction silentlycontinue
Start-SPAssignment -Global
#Recursive Function to read through all the files and folders till the leaf node
function Recurse([string]$path, $web, $docLibrary,$drive) {
$objFStream = new-object -com scripting.filesystemobject
#Fetching the current folder name in the network drive path
$folder = $objFStream.getfolder($path)
$fldPath = $path.Replace($drive, ”).Replace(‘\’, ‘/’)
#I am removing the current folder name to create destination folder path to upload the files.
$desPath = ($fldPath + ‘$$’).Replace($folder.Name + ‘$$’, ”)
Write-Host “Exporting data from folder :: “$folder.Name
#Validating for the existence of the folder, if the folder is not existed in Library I am creating the folder in document library
$objFldVald = $web.GetFolder($docLibrary.RootFolder.ServerRelativeUrl + $fldPath);
if ($objFldVald.Exists -eq $false) {
$docFldCreate = $docLibrary.Items.Add($docLibrary.RootFolder.ServerRelativeUrl.TrimEnd() + $desPath.TrimEnd(‘/’), [Microsoft.SharePoint.SPFileSystemObjectType]”Folder”, $folder.Name);
$docFldCreate.Update()
}
#Reading all the files in the current folder
foreach ($objFile in $folder.files) {
#Create file stream object from file
$fileStream = ([System.IO.FileInfo] (Get-Item $objFile.Path)).OpenRead()
$contents = new-object byte[] $fileStream.Length
$fileStreamLength = $fileStream.Read($contents, 0, [int]$fileStream.Length);
$fileStream.Close();
#Add file to document library in the same folder as in Network drive.
$DocLibFld = $web.getfolder($docLibrary.RootFolder.ServerRelativeUrl.TrimEnd() + $fldPath)
[Microsoft.SharePoint.SPFile]$spFile = $DocLibFld.Files.Add($DocLibFld.Url + “/” + $objFile.Name, $contents, $true)
Write-Host “File =” $objFile.Name “`tUploaded to ” $web.Url”/”$DocLibFld
}
#Checking if there any sub folders in the current folder if there any I am calling the Rucrse loop to process the folder.
foreach ($objSubFld in $folder.subfolders) {
Recurse $objSubFld.path $web $docLibrary $drive
}
}
#Enter the site URL.
$webUrl = “http://sharepoint13:12345/”
$web = Get-SPWeb -Identity $webUrl
#Mention the document library name in which the files need to be uploaded.
$docLibrary = $web.Lists[“Documents”]
#Provide the path of the local folder to be migrated to SharePoint.
Recurse “S:\Export Data” $web $docLibrary ‘S:’
Write-Host “Migration Completed Sucessfully …!!!”
$web.Close()
Stop-SPAssignment -Global
}
catch {
Write-Host “`n Error:: $($_.Exception.Message)” -ForegroundColor Red -BackgroundColor Yellow
}

The power of this script is:
– It can upload any type of files and I have tested with

  • Word Documents
  • Excel Files
  • PDF
  • PPT documents
  • Microsoft Project Planner documents
  • Image files
  • Text files
  • No limit on the depth of the folder
  • No limit on the number of files in a folder to be uploaded
  • NO HARD CODING OF ANY LIMITS / DOCUMENT TYPES

The output for the above script:

PowerShell to Migrate files & folder structure in SharePoint tutorial
PowerShell to Migrate files & folder structure in SharePoint tutorial

This is how to migrate files in SharePoint 2013 with folder structure using PowerShell.

You may like following PowerShell SharePoint tutorials:

In this SharePoint tutorial, we learned, how to upload files to SharePoint using PowerShell? And how to upload large files to sharepoint 2013/2016/2019.

  • Hi Krishna, I can upload files to the Documents folder and all the files go directly there, however how would I upload directly to a sub folder or any other folders in that location? I wasn’t sure of the command to add

  • The files go into SharePoint Documents location, just trying to figured out what needs to be added to file folders within for files to go there instead.

    Before:
    $DocLibName = “Documents”

    Need something like:
    $DocLibName = “DocumentsOtherFolder”
    $DocLibName = “DocumentsOtherFolderMore Folders”

    Please let us know your thoughts, thanks Krishna for all the info you’ve provided already by the way!

  • I think I found out my answer after doing some more research…

    $SubFolderName = “TempTestingFolder”

    $FolderToBindTo = $List.RootFolder.Folders
    $Context.Load($FolderToBindTo)
    $Context.ExecuteQuery()
    $FolderToUpload = $FolderToBindTo | Where {$_.Name -eq $SubFolderName}

    $Upload = $FolderToUpload.Files.Add($FileCreationInfo)

    Thanks for providing a base!

  • Error:: Exception calling “UploadFile” with “3” argument(s): “The remote server returned an error: (405) Method Not Allowed.”

    i’m getting this error when i try to upload file to sharepoint using first script mentioned in blog

  • >