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

Comments (7)
  1. I recently wrote a post that details how to upload content to SharePoint 2013 remotely using CSOM, whilst the example is for O365 it could easily be adapted for On-Prem –

  2. Ben Oster says:

    This is going to be incredibly handy for me as I have a couple of users who have to upload the same document to multiple locations with different names for each location.  I'm going to adapt this concept to upload straight from their computer with the $Output effectively becoming $SourceFile.  That's the easy part to add to this script.  Here's my question…

    What if the $Output has to be a .pdf?

    Thanks in advance!


  3. This script has to be run directly from a SharePoint server as it uses the object model, if I get chance next week will include an example that can be run remotely.

  4. Steve Roe says:

    Any chance you ever came up with the script to run this remotely? If not, that’s what I will be doing over the next couple days and can post back.

  5. Bill Fox says:

    Steve, I would love to see what you come up with. If you see this, please post your result here! 🙂

  6. Andy Jackson says:

    You can also use UNC naming to output to a Library directly. For example:

    $somecommand | Out-File “\\\Sites\Site\Library” -encoding Ascii

    This will also work on premise.

  7. James Walker says:

    I am looking for information as to how to build a user form so the user can enter in various data information in SharePoint, have the inputted data then get save to a .csv file and have the file moved to a network location for another application to pick up and process. Got any suggestions?

Comments are closed.

Skip to main content