FromTheField

Real world experiences of SharePoint PFE and CTS engineers from Microsoft UK

Using PowerShell to upload a scripts output to SharePoint

Using PowerShell to upload a scripts output to SharePoint

  • Comments 5
  • Likes

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 = "http://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
  • 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

  • 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.

  • 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.

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

  • 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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment