Tuesday, April 10, 2012

Upload file to SharePoint document library using PowerShell

Following small script will upload file to a given document library in SharePoint

  1. $webUrl = "https://sp13/sites/site"
  2. $docLibName = "SiteLog"
  3. $filePath = "C:\Logs\test.txt"
  4.  
  5. #upload file
  6. $web=Get-SPWeb $webUrl
  7. $file=Get-Item $filePath
  8. $fileStream=([System.IO.FileInfo](Get-Item $file.FullName)).OpenRead()
  9. $folder=$web.GetFolder($docLibName)
  10. $spFile=$folder.Files.Add($folder.Url +/” + $file.Name, [System.IO.Stream]$fileStream, $true)
  11. $fileStream.Close()