How to upload Documents in a folder to SharePoint list in Office 365/SharePoint 2010/2013/2016/2019?

Hello Sharepointers,

Below are the powershell script to upload Documents in a folder to SharePoint list in Office 365/SharePoint 2010/2013/2016/2019.

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
    Write-Host "SP Add-in Activated"
}
 

 
$webUrl = "https://lakssite"
 
$docLibraryName = "Employee"
$docLibraryUrlName = "Emp" 
$localFolderPath = "C:\Employee Folder"
 
 
$web = Get-SPWeb $webUrl
Write-Host "webSite = " + $webSite
 
$docLibrary = $web.Lists[$docLibraryName]
 
$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles()
 
ForEach($file in $files)
{
 
    #Open file
    $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()
 
    #Add file
    $folder =  $web.getfolder($docLibraryUrlName)
 
    write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."
    $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
    write-host "Success"
 
    #Close file stream
    $fileStream.Close();
}
 

 
$web.Dispose()

Leave a Reply

Your email address will not be published. Required fields are marked *