Use Windows PowerShell to Manage Lists in SharePoint 2010

ScriptingGuy1

Summary: Learn how to use Windows PowerShell to manage lists in SharePoint 2010. Today’s guest blogger is Glyn Clough.

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I use Windows PowerShell to work with SharePoint 2010 lists?

— GC

 

Hey, Scripting Guy! Answer

Hello GC,

Microsoft Scripting Guy Ed Wilson here. This is Guest Blogger Week and we are continuing with SharePoint today via Glyn Clough.

Photo of Glyn Clough

Glyn Clough is a SharePoint consultant working for a Microsoft Gold Partner in London. He has a Master’s degree in Computer Science from Oxford University and has worked for Content and Code, a Microsoft Gold Partner, in London for the past four years. With seven years of experience working with SharePoint, Glyn is passionate about learning all there is to learn about the product. Find him at his blog, GlynBlogs.com, and on Twitter at @GlynClough.

Here’s Glyn…

 

Microsoft SharePoint is a big product. And I mean really big. But just when you thought it couldn’t grow any more, Microsoft has now made it even larger with the SharePoint 2010 release. Not only have they added a host of new and extended capabilities from the previous version, but also they have provided built-in support for Windows PowerShell—exposing more than 500 specific SharePoint cmdlets.

This is great news for IT pros and system administrators who are working with SharePoint on a regular basis. For example, the SharePoint Community has already created an automated SharePoint farm installer, which is available on CodePlex. This consistent provisioning mechanism is also of huge benefit to developers who can create (and destroy!) temporary environments readily without the overhead of having to manually run through a re-installation every time. So far I’m not highlighting anything new in pointing out that Windows PowerShell and SharePoint are a perfect match.

However, what may not be quite so obvious is that Windows PowerShell is also a great tool for non-IT pros or developers. As a consultant whose responsibilities touch on several different disciplines in a project, it’s often essential to have a clear picture of the current state of the SharePoint environment and a thorough understanding of vanilla installations. That’s where I’ve been seeing Windows PowerShell act as a fantastic productivity tool. By using a few simple cmdlets in the SharePoint 2010 Management Shell, you can easily extract important details that would otherwise have taken an age of trawling through the front-end UI or using third-party tools to find. I’ve previously posted on listing all active features in a SharePoint site collection, and now I’d like to show how it’s possible to work with another of the fundamentals in any SharePoint solution: lists.

I’ve used the included Get-SPSite and Get-SPWeb cmdlets to access the list objects from a particular site collection, which can then be output to display the lists and their properties in use on the site. Rather than having to type these lines repeatedly, I’ve created some straightforward functions and stored them in a PS1 file. This file can then be loaded in the SharePoint 2010 Management Shell when needed and thus make the functions available for use. The functions I’ve created are shown in the following table.

Function

Description

Get-SPList

Returns information about a specific list from a URL and title, and displays the list properties. The schema XML can be optionally returned by passing in a flag to the function.

Get-SPLists

Returns a table of lists in a given site collection from a URL. “Common” lists (such as the master page gallery) and empty lists are not included in the results by default, but this can be overridden by passing in flags to the function.

Get-SPListsByTemplate

Returns a table of the types of lists in use in a site collection from a URL, and includes a count of the number of instances. “Common” lists (such as the master page gallery) and empty lists are not included in the results by default, but this can be overridden by passing in flags to the function.

The script for these functions can be found on the TechNet Script Center Repository in the SharePoint category; however, here are some screenshots of running these functions against an out-of-the-box publishing portal.

The following image shows Get-SPList for the Pages library:

Image of Get-SPList for Pages library

The following image shows Get-SPLists hiding common lists and including empty lists by using the –HideEmptyLists flag:

Image of Get-SPLists hiding common lists and including empty lists

And the following image displays Get-SPListsByTemplate hiding common lists and empty lists (by default).

Image of Get-SPListsByTemplate hiding common lists and empty lists by default

In the above screenshots, you can see how straightforward it is to load the functions from an external file (by
using “. .\SPListFunctions.ps1”), and then call the desired function with the correct parameters. The results are output to the SharePoint 2010 Management Shell window, but could just as easily be written to a file somewhere on the file system if required. I find these functions useful when working on solutions with custom list definitions, and we need to know where and how they’re being used. Using the Get-SPLists function can also be a quick way to find empty lists on the site.

With a bit more work, I’m sure the functions could be converted into more robust and extensible custom cmdlets, but even as simple functions, they give an indication of just how incredibly useful Windows PowerShell is when combined with SharePoint to interrogate environments and access otherwise hidden information. And of course it is possible to go much further than simply reporting back and to interact with your SharePoint environment as the automated installer shows us. Windows PowerShell skills are rapidly becoming a must-have for any SharePoint professional.

 

GC, that is all there is to using Windows PowerShell to manage lists in SharePoint 2010. Guest Blogger Week will continue tomorrow when we will have Microsoft MVP Sean Kearney with us. We invite you to follow us on Twitter and Facebook. If you have any questions, send email to us at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

0 comments

Discussion is closed.

Feedback usabilla icon