Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null
#Function to Upload File
function UploadAllFilesFromDir($WebURL, $DocLibName, $FolderPath)
{
#Get the Web & Lists to upload the file
$site = New-Object Microsoft.SharePoint.SPSite($WebURL)
$web= $site.OpenWeb()
#Get the Target Document Library to upload
$List = $Web.GetFolder($DocLibName)
#Get the Files from Local Folder
$Files = Get-ChildItem $FolderPath #You can filter files by: -filter “*.pdf”
#upload the files
foreach ($File in $Files)
{
#Get the Contents of the file to FileStream
$stream = (Get-Item $file.FullName).OpenRead()
# Set Metadata Hashtable For the file - OPTIONAL
$Metadata = @{"Country" = "India"; "Domain" = "ASIA"}
#upload the file
$uploaded = $List.Files.Add($File.Name, $stream,$Metadata, $TRUE)
#dispose FileStream Object
$stream.Dispose()
}
#Dispose the site object
$site.Dispose()
}
#call the upload function
UploadAllFilesFromDir "<<Sharepoint Site URL>>" "Library Name" "\\Server Name\D$\Documents\"This code snippet is for bulk upload files to SharePoint using PowerShell.
