Jaa


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 = "https://intranet.contoso.com/Sites/IT"
$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
$FileCollection.Add($File.Name,$Stream,$true)
$Stream.Close()
$File.Delete()

Brendan Griffin

Comments

  • Anonymous
    January 01, 2003
    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 - http://blogs.technet.com/b/fromthefield/archive/2014/02/19/office365-script-to-upload-files-to-a-document-library-using-csom.aspx

  • Anonymous
    August 23, 2013
    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! Ben

  • Anonymous
    August 25, 2013
    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.

  • Anonymous
    February 12, 2014
    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.

  • Anonymous
    February 18, 2014
    Steve, I would love to see what you come up with. If you see this, please post your result here! :)

  • Anonymous
    August 30, 2016
    You can also use UNC naming to output to a Library directly. For example:$somecommand | Out-File "\nameoforg.sharepoint.com@SSL\Sites\Site\Library" -encoding AsciiThis will also work on premise.

  • Anonymous
    September 22, 2016
    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?