Get Started Managing SharePoint 2010 with PowerShell Cmdlets



Summary: Scripting Guys guest blogger Niklas Goude shares essential how-to information about using the Windows PowerShell cmdlets to manage SharePoint 2010.


Hey, Scripting Guy! QuestionHey, Scripting Guy! I am interested in getting started with using Windows PowerShell cmdlets to manage SharePoint 2010. Do you have any words of wisdom?

— GN


Hey, Scripting Guy! Answer Hello GN,

Microsoft Scripting Guy Ed Wilson here. Yes I have words of wisdom: “Ask the guy who wrote the book.” We have Niklas Goude with us this week to share his wealth of knowledge. Niklas, we turn the blog over to you this week.

Niklas Goude is a Windows PowerShell MVP working at Enfo Zipper in Stockholm, Sweden. Niklas has extensive experience in automating and implementing SharePoint environments using Windows PowerShell. He has written a Windows PowerShell book for Swedish IT pros,, and is currently co-authoring a book with Mattias Karlsson titled, PowerShell for Microsoft SharePoint 2010 Administrators, which will be published in English by McGraw-Hill in October 2010. Parts of this post are taken from Chapters 3 and 4 of that book.

Niklas also runs the blog,, where he shares scripts, examples, and solutions for administrative tasks in Windows environments through Windows PowerShell.


Getting Started

Windows SharePoint is one of the fastest growing Microsoft products in history, and it is quickly becoming mission critical for many companies around the world. Whereas SharePoint 2007 was a really cool product with an automation API, its use for automation purposes was a bit complicated for the average SharePoint administrator. This is where the inclusion of Windows PowerShell as a management tool for SharePoint 2010 comes in to play.

In SharePoint 2010, you can start Windows PowerShell through the SharePoint 2010 Management Shell. The shell runs the SharePoint.ps1 script at startup and executes the following code:

$ver = $host | select version
if ($ver.Version.Major -gt 1) {$Host.Runspace.ThreadOptions = “ReuseThread”}
Add-PsSnapin Microsoft.SharePoint.PowerShell
Set-location $home

The code in the preceding example stores the host’s version in a variable, and if the major version is greater than one (if you are running Windows PowerShell 2.0), the ThreadOptions property is set to “ReuseThread”, which runs each line, function, or script on the same thread. When working with the SharePoint object model using Windows PowerShell, running code on separate threads can cause memory leaks, but commands running on the same thread have a smaller chance of doing so. This is not only because some SharePoint objects still use unmanaged code, but also the way memory is allocated to those objects. Next, the SharePoint snap-in is loaded (Microsoft .NET Framework assemblies that may contain custom Windows PowerShell cmdlets).

The SharePoint 2010 Cmdlets

The SharePoint 2010 snap-in for Windows PowerShell contains more than 500 cmdlets that you can use to perform a large variety of administrative tasks. Let’s see how we can list all the SharePoint cmdlets using the Get-Command cmdlet. Get-Command returns basic information about cmdlets and other elements of Windows PowerShell commands, such as functions, aliases, filters, scripts, and applications. All nouns of the SharePoint 2010 cmdlets start with SP. Knowing this, we can use the –noun parameter of the Get-Command cmdlet followed by SP*:

PS > Get-Command -noun SP*

The results of this command are shown in the following image.

Image of results of this command

The list of cmdlets returned is pretty long. You can also use Get-Command to find specific SharePoint 2010 cmdlets. For example, if you want to find all cmdlets that are used to manage site collections, you can simply type:

PS > Get-Command -Noun SPSite

This will produce the following results:

CommandType                 Name                              Definition

——————                  ——–                             ————

Cmdlet                              Backup-SPSite                Backup-SPSite [-Identity] <SPSitePip

Cmdlet                              Get-SPSite                      Get-SPSite [-Limit <String>] [-WebAp

Cmdlet                              Move-SPSite                   Move-SPSite [-Identity] <SPSitePipeB

Cmdlet                              New-SPSite                     New-SPSite [-Url] <String> [-Languag

Cmdlet                              Remove-SPSite               Remove-SPSite [-Identity] <SPSitePip

Cmdlet                              Restore-SPSite                Restore-SPSite [-Identity] <String>

Cmdlet                              Set-SPSite                       Set-SPSite [-Identity] <SPSitePipeBi

Working with SharePoint 2010 Cmdlets

Let’s see what we can do with the Get-SPSite cmdlet. Typing the cmdlet in Windows PowerShell returns the site collections available:

PS > Get-SPSite



Notice how the command returns the site collections URL property, although the returned objects have a lot more properties. The properties displayed by default are controlled by a set of formatting files. Windows PowerShell includes 10 formatting files, and SharePoint 2010 comes with 13 additional formatting files that are used to generate a default display of various .NET Framework objects.

We can display additional properties using the Select-Object cmdlet. In the example below, we use the –Identity parameter supported by the Get-SPSite cmdlet to retrieve a specific site collection and pipe the object to the Select-Object cmdlet:

PS > Get-SPSite -Identity http://SPServer | Select-Object -Property Url, Zone, Port

Url Zone Port

 —- —-

http://spserver Default 80

It’s also possible to change specific properties of a site collection. First let’s see how to we can add a secondary contact to the site collection using the Set-SPSite cmdlet:

PS > Get-SPSite -Identity http://SPServer | 
>> Set-SPSite -SecondaryOwnerAlias domain\user

If we use the Select-Object cmdlet again and display the SecondaryContact property, we’ll see that a secondary contact is added to the site collection:

PS > Get-SPSite -Identity http://SPServer | Select SecondaryContact




You can also store an object of the type SPSite in a variable and set the SecondaryContact property. The property requires an object of the type Microsoft.SharePoint.SPUser, which is just the type of object that the Get-SPUser cmdlet returns. Note that the user has to exist in the site collection:

PS > $spSite = Get-SPSite -Identity http://SPServer
PS > $spSite.SecondaryContact = 
>> (Get-SPUser -Web http://SPServer -Identity domain\user)

What if you want to add a user that exists in Active Directory but does not exist in the site collection? Simply use the New-SPUser cmdlet to add a user to a site collection and then add the object to the SecondaryContact property:

PS > $spUser = New-SPUser -Web http://SPServer -UserAlias domain\newuser
PS > $spSite.SecondaryContact = $spUser

When we are done with the object stored in the $spSite variable, it’s important to dispose of it correctly. One way of doing this is by calling the Dispose() method:

PS > $spSite.Dispose()

Why do we have to dispose of the object? Well, SPWeb, SPSite, and SPSiteAdministration objects can sometimes take up large amounts of memory, so using any of these objects in Windows PowerShell requires proper memory management. Normally, instances of these objects obtained through cmdlets such as Get-SPSite are disposed of automatically at the end of the pipeline, but this does not happen to instances stored in variables. You can dispose of objects using the Dispose() method as demonstrated in the preceding example, or you can use the Start-SPAssignment and Stop-SPAssignment cmdlets that were introduced in SharePoint 2010 to spare scripters the need to dispose of such objects individually.


In this post, we have seen examples about how to find and work with the SharePoint 2010 cmdlets. There is, of course, a lot more cool stuff that you can do when using Windows PowerShell to automate your SharePoint 2010 environment, such as managing content databases, web applications, and sites. We also took a brief look at how we can work with site collections in SharePoint 2010 with examples about retrieving and modifying site collections. It’s also possible to create new site collections, back up and restore site collections, and move and remove site collections.

GN, that is all there is to getting started with SharePoint 2010 and Windows PowerShell. Guest Blogger Week with Niklas Goude will continue tomorrow.

We would love for you to follow us on Twitter and Facebook. If you have any questions, send email to us at, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.


Ed Wilson and Craig Liebendorfer, Scripting Guys


Discussion is closed.

Feedback usabilla icon