Upload documents from network drive to SharePoint 2013/2016 Server document library using PowerShell

This PowerShell SharePoint tutorial explains, how to upload documents from network drive to SharePoint 2016/2013 document library using PowerShell.

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.

Upload documents from network drive to SharePoint document library using PowerShell

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>>”
upload documents from network drive to SharePoint server docuemnt library using 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

Files uploaded to SharePoint Library:

upload file to sharepoint document library powershell

You may like following PowerShell SharePoint tutorials:

This SharePoint tutorial we learned, how to upload documents from local drive to sharepoint 2016/2013 document library using PowerShell

free sharepoint training

SharePoint Online FREE Training

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

envelope
envelope

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

>