Upload Multiple documents(Files) to SharePoint Document Library using PowerShell

Note: Please do not copy paste the complete URL of the document library (which includes allitems.aspx) instead copy till document library , ex: http://SharePoint/DocumentLibraryName

param ([String]$spPath, [String]$localPath)
## Load the SharePoint Snapin so the script can be executed from PowerShell editor
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
# get the SharePoint path if not specified
if ($spPath -eq “”)
{
    Write-Host
   $spPath = Read-Host -Prompt “Please enter the fully qualified url to the destination document library”
}
# get the local path if not specified
if ($localPath -eq “”)
{
    Write-Host
    $localPath = Read-Host -Prompt ” Please enter the full path to the local directory that contains the documents to be uploaded”
}
Write-Host
## Define our library name and web url
$LibraryName = $spPath.Substring($spPath.LastIndexOf(“/”)+1)
$webUrl = $spPath.Substring(0,$spPath.LastIndexOf(“/”))
## Define a function to return the destination file name
Function BuildFileName($filename)
{
    “$webUrl/$LibraryName/” + $(split-path -leaf $filename)
}
## Upload all the documents found in the specified location
Write-Host “Uploading all documents from $sourceDocs”
$webClient = New-Object System.Net.WebClient
$webClient.Credentials = [System.Net.CredentialCache]::DefaultCredentials
dir $localPath | % {
    Write-Host “Uploading: $_” -nonewline
    $fName = BuildFileName $_
    $webClient.UploadFile($fName,”PUT”, $_.FullName)
    Write-Host ” done” -ForegroundColor Green
    }

By Indra

SharePoint Architect

Leave a Reply