Upload documents from network drive to SharePoint 2013 or SharePoint 2016 Server docuemnt library using PowerShell

upload multiple files to sharepoint using powershell

Here we will discuss how we can upload document from network drive to SharePoint 2016/2013 document library using PowerShell.

Scenario:

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:

I have written a PowerShell script to upload all the user documents to 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:

Read SharePoint 2013 tutorials

Please follow the below steps to execute the PowerShell script:



1. Log on to Application Server (where you can see Central admin)
2. Open SharePoint 2013 management shell as administrator
3. 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:\
4. Navigate on to c:\ in Management shell
5. Execute the script by typing “.\<<FileName>>”

powershell upload file to sharepoint online
powershell upload file to sharepoint online

6. This will upload all the file in “C:\Temp\PS Testing\T” folder

Output:

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

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

Output in Management Shell:

powershell upload file to sharepoint document library
powershell upload file to sharepoint document library

Files uploaded to SharePoint Library:

upload multiple files to sharepoint using powershell
upload multiple files to sharepoint using powershell

Hope this article will help you to upload documents from local drive to sharepoint 2016/2013 document library.

Similar SharePoint 2013 Tutorials


Leave a Reply