Output to SharePoint List Using PowerShell


Those of us who manage SharePoint Farms have inevitably been tasked with running PowerShell scripts that need data written outside of the PowerShell console. The most common output method is to a .txt or .csv file. That’s all well and good, but wouldn’t it be cool to send that output straight to a SharePoint list? It turns out, it’s really quite easy to do. Below, I hope to provide an example of how to do this.

STEP 1 – Establish SharePoint List Variable

**NOTE** You will need to already have a list created with the columns you plan to use in your output prior to running the script

#Region SP Output File Variables
$webURL = "http://yourdomain"
$listName = "List Name"

#Get the SPWeb object and save to variable
$listWeb = Get-SPWeb $webURL

#Get the SPList object to retrieve the list
$list = $listWeb.Lists[$listName]
#endRegion

STEP 2 – Establish Web Application Variable

In this script snippet you as a user are asked to enter the URL of the Web Application to run the scrip against. You can certainly hard code this to automate the script, but I prefer when offering scripts, to never hard code variables but rather leave it open for others to decide. This is why step 1 above has the two variables hard coded and in this step it is shown as a user input option. You can choose either or and modify the code as needed.

#Region Web Application Variable
$Siteurl = Read-Host "Enter Web Application URL"
$Rootweb = New-Object Microsoft.Sharepoint.Spsite($Siteurl);
$Webapp = $Rootweb.Webapplication;
#endRegion

STEP 3 – Write to SharePoint List

Now that you’ve instantiated the list above, you can now begin writing to that list. I will use the following example for this example code.

Example: Export the following information for each site collection in a web application

  • Site Name – (Single line of text)
  • Site URL – (Hyperlink or Picture)
  • Primary Site Collection Administrator – (Single line of text)
  • Secondary Site Collection Administrator – (Single line of text)
#Region Write to SP List

#Loops through each collection within the web application
Foreach ($Site in $Webapp.Sites)
{
#Create new item
$newItem = $list.Items.Add()

#Web variable used to get Site Collection Title
$web = Get-SPWeb $Site.URL

#Add properties to this list item
$newItem["Title"] = $web.Title
$newItem["Site URL"] = $Site.Url
$newItem["Primary Site Collection Administrator"] = $Site.Owner.Name
$newItem["Secondary Site Collection Administrator"] = $Site.SecondaryContact.Name

#Update the object to publish to list
$newItem.Update()
}
#Dispose
$Site.Dispose();
$web.Dispose();
#endRegion

Here is what your results can look like below

List Image

If you would like to download this script in its entirety you can get the Example Output to SP.ps1 here. If you have questions please leave a comment here. Also, if you have a way of writing the Administrator info into the Person fields let me know. I didn’t have time to figure that out. I can just use a workflow to do it, but if I could just write directly to a Person field that would be ideal.

Advertisements

4 thoughts on “Output to SharePoint List Using PowerShell

    • Glad this is inline with something you’re working on. We’ve been using it for reporting our Site Collection sizes and quantities as well as the number of sub sites as our farm is growing quite a bit every month. Thanks for stopping by and commenting.

      • This is a great way to give SharePoint admins an easy tool (SharePoint) to create dashboards or individual views on the stuff they have to keep an eye on. We’ve been monitoring database sizes, creating site collection overviews, monitoring timer job status, listing deactivated timer jobs, sites that have certain third party features activated, crawl history. If you have PowerShell skills and too much time you can do really fancy things 🙂

      • Ha ha… I like your last sentence. You’re right though, there are a lot of really cool things you can do. I manage a small Foundation farm for a fairly large org. We have about 20k users of the total 60k+ employees using the farm now and are working to deploy a Standard/Enterprise 2013 farm now. PowerShell has been invaluable for bulk maintenance and other tasks that just can’t be done via the interfaces provided. I only wish I had more time to explore and play more with it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s