Use SharePoint 2010 PowerShell Cmdlets to Get and Manage Sites



Summary: The Microsoft Scripting Guys host guest blogger Niklas Goude as he discusses site management using Windows PowerShell cmdlets.


Hey, Scripting Guy! Question Hey, Scripting Guy! How do I manage sites in SharePoint 2010 by using Windows PowerShell cmdlets?

— KM


Hey, Scripting Guy! Answer Hello KM,

Microsoft Scripting Guy Ed Wilson here, once again I will defer the question to our guest blogger for the week, Niklas Goude.

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 Chapter 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 Sites in SharePoint 2010

Let’s see how we can manage Sites in a SharePoint 2010 environment. As discussed in yesterday’s post, SharePoint 2010 includes more than 500 cmdlets. Some of these are designed to manage site collections and sites. A site in SharePoint 2010 is hosted within a site collection and contains one or more web pages, lists, or libraries to store and present information. The first cmdlet we’ll look at is Get-SPWeb, which is used to retrieve one or more sites. Here’s an example of using the cmdlet:

PS > Get-SPWeb -Identity http://SPServer



The Get-SPWeb cmdlet displays the Url property by default. We can display all properties using the Format-List cmdlet as demonstrated in the following image.

Image of Format-List cmdlet displaying all properties

The example above displays a lot of cool properties such as Title, Description, Language, when the site was created, and when the last item was modified.

Modifying Sites in SharePoint 2010

If we want to modify specific properties, we can use the Set-SPWeb cmdlet. Here’s how to change the description of a site:

PS > Get-SPWeb -Identity http://SPServer | Set-SPWeb -Description “PowerShell is Cool”

You can use the Set-SPWeb cmdlet to set some of the properties. At some point you’ll want to change additional properties that are not supported by the Set-SPWeb cmdlet. You can do this by storing a site in a variable and then set the properties:

PS > $spWeb = Get-SPWeb -Identity http://SPServer

PS > $spWeb.Title = “PowerShell”

PS > $spWeb.TreeViewEnabled = “True”

PS > $spWeb.Update()

In the example above, we change the sites title and enable the TreeView. Finally, we use the Update() method to commit the changes. Just as with objects of the type SPSite, SPWeb objects need to be disposed of correctly when stored in variables using either the Dispose() method or the Start-SPAssignment and Stop-SPAssignment cmdlets. The example below demonstrates how to use the Dispose() method:

PS > $spWeb.Dispose()

Windows PowerShell makes it real simple to update multiple sites in a site collection through a couple of lines of code. Here’s how to enable the TreeView on all sites in a site collection:

PS > Get-SPSite -Identity http://SPServer | Get-SPWeb | ForEach-Object {

>> $_.TreeViewEnabled = “True”

>> $_.Update()

>> }

In the example above we use the ForEach-Object cmdlet to iterate through each site in the site collection and enable the TreeView and finally call the Update() method to commit the changes.

Creating Sites in SharePoint 2010

Creating a new site is a simple procedure when using Windows PowerShell. Simply use the New-SPWeb cmdlet as demonstrated below.

PS > New-SPWeb -Url http://SPServer/NewWeb -Template “STS#0” -Name “New Site” `
>> -Description “My New Site”



In the example above we set the site’s new URL and set the Template to “STS#0”, which corresponds to a team site. We also set the Sites Name and Description.

Backup & Restore Sites in SharePoint 2010

Let’s take a closer look at backup and sestore. When working with sites, we can use the Export-SPWeb cmdlet to export a site:

PS > Export-SPWeb -Identity http://SPServer/NewSite `

>> -Path C:\Backup\spWebBackup.bak

This example exports an entire site to a backup file. It is also possible to export specific content from a subsite, such as lists, document libraries, and list items. You use the ItemUrl parameter to export lists or list items from a subsite. Here is an example of exporting a list called Calendar from a site:

PS > Export-SPWeb -Identity http://SPServer/NewSite `

>> -ItemUrl “Lists/Calendar” -Path C:\Backup\spWebCalendar.bak

The Export-SPWeb cmdlet also supports the IncludeUserSecurity switch parameter, which allows you to include access control lists for all items. By default, Export-SPWeb exports the last major version of a list item, but you can change this by setting the IncludeVersions parameter to include the current version, last major and minor version, or all versions of each item.

After you have an export file, you can use the Import-SPWeb cmdlet to import it into a subsite. Importing a subsite works as long as you specify a site collection that contains a matching template; otherwise, an error occurs:

PS > Import-SPWeb -Identity http://SPServer/NewSite -Path C:\Backup\spWebCalendar.bak

The Import-SPWeb cmdlet also supports the UpdateVersions parameter, which allows you to specify how to handle items that already exist in a list. The possible values are Append, Overwrite, and Ignore.

Remove Sites in SharePoint 2010

Finally, let’s take a look at how to remove an existing site. The Remove-SPWeb cmdlet removes a specific site from SharePoint 2010. If the top-level site is deleted, the site collection is also removed. Here is an example of running this cmdlet:

PS > Remove-SPWeb -Identity http://SPServer/NewSite -Confirm:$false

In this post we’ve covered sites in SharePoint 2010, with examples showing us how to create, modify, backup and restore, and remove sites in SharePoint 2010. It’s also possible to go one step further and automate the creation of sites using a user-friendly SharePoint list, manage lists and libraries, and even items. We’ll see examples of this in the upcoming post. You should also check out PowerShell for Microsoft SharePoint 2010 Administrators for a detailed description of how you can manage your SharePoint 2010 environment using Windows PowerShell. (This book will be available in October 2010.)

KM, that is all there is to using Windows PowerShell cmdlets to manage sites in SharePoint 2010. Guest Blogger Week with Niklas Goude and SharePoint will continue tomorrow.

We would love for you to follow us on Twitter and Facebook. If you have any questions, send e-mail 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