Using PowerShell to upload a scripts output to SharePoint

I often get asked to help customers write PowerShell scripts to aid with the reporting and administration of their SharePoint environment and as you can probably see from my previous Blog posts, this is something I rather enjoy! Recently a customer asked me to help them write a script that would extract information from SharePoint (for example a list of Sites) and then upload this information to a SharePoint document library. This was actually a lot easier than I anticipated and I thought I would share :)

Please find the script below, this has been tested on SharePoint 2010 and should work on 2013 too. the highlighted values need to be updated to reflect your environment -

  • $Output = The location to store the actual output of the script, this is deleted once the output file has been uploaded to SharePoint.
  • $WebURL = The web that contains the list that you would like to upload the output file to.
  • $ListName = The name of the document library to upload the output file to
  • Get-SPSite etc = Replace this with commands to extract the relevant information from the farm, in this example I'm simply reporting a list of sites for illustration purposes, which is probably as basic as you can get!


ASNP *SharePoint* -EA SilentlyContinue
#Declare Variables
$Output = "D:\Logs\Output.txt"
$WebURL = ""
$ListName = "SharePointInfo"

#Create something to upload, in this case a list of all sites
Get-SPSite | Out-File -FilePath $Output
#Upload the results to SharePoint
$File = Get-Item $Output
$Stream = $File.OpenRead()
$Web = Get-SPWeb $WebURL
$List = $Web.Lists["$ListName"]
$FileCollection = $List.RootFolder.Files

Brendan Griffin