Use SharePoint 2010 PowerShell Cmdlets to Get and Manage Sites

Use SharePoint 2010 PowerShell Cmdlets to Get and Manage Sites

  • Comments 8
  • Likes

 

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, powershell.se, 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, powershell.nu, 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

Url

---

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"

Url

---

http://spserver/NewWeb

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
Summary

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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • I apologize if this is a newbie question... but.. is there anyway to get this information if you're not a SharePoint administrator? For example, I have a SharePoint site and am the site admin... but not the server admin. I'm big on automation and love to play with new things. Is there any way for me to access these, or, how do I get them?

    ~Tully

  • Hi,

    Running the SharePoint 2010 cmdlet in Windows PowerShell requires that you (the account you are using) are member of the SharePoint Shell Access Role in the Configuration Database and the WSS_ADMIN_WPG local Security Group on the SharePoint Server.

    You can use the Add-SPShellAdmin cmdlet to add a user to the SharePoint Shell Access Group and the WSS_ADMIN_WPG local security group. example:

    PS > Add-SPShellAdmin -UserName domain\user `

    >> -database (Get-SPContentDatabase -Identity WSS_Content)

    Regards

    Niklas Goude

  • In your new site example you mantion the command

    New-SPWeb -Url http://SPServer/NewWeb -Template "STS#0" -Name "New Site" -Description "My New Site"

    This is a similar command to others I have seen provided by MS that all reference the "STS#0" as the site template. I have a few questions regarding this.

    1. How do you determine what the correct "STS#0" or other number for any given template is?

    2. I am familiar with the process of adding templates to the global store in MOSS 2007 using

    stsadm -o addtemplate

    and then retrieving the global template number by using

    stsadm -o enumtemplates

    However I do not see this same functionality in SP2010. I have used the stsadm -addsolution and enumsolution but I have not been able to successfully create a site using a stored global template?

    3. I am trying to automate site creation using STSADM but because I can not successfully add the custom template/solution the command completes but on first visit to the site I have to choose the template. The command I am using to do this is.

    stsadm -o createweb -url http://sp2010sandbox/Joe -sitetemplate template.wsp -title "Joes Sharepoint Site" -unique

    Any help is appreciated and thanks for your assistance.

  • Hi, is there a script that will list the active user profile on the sharepoint 2010?

    Thanks

  • Hi,

    Nice blog. I can't find any explanation of what the -Recycle operator does? Any clue? IIS maybe?

    Remove-SPWeb [-Identity] <SPWebPipeBind> [-AssignmentCollection <SPAssignmentCollection>] [-Confirm [<SwitchParameter>]] [-Recycle <SwitchParameter>] [-WhatIf [<SwitchParameter>]] (re: technet.microsoft.com/.../ff607891.aspx)

  • In SP2010 server / enterprise

    here is a working example of a real export-spweb pull of a site list using the -nofilecompression switch so you can use granular repair to import the groups and users that may not get imported properly.  We had several bugs upgrading a 2003/2007 hybrid WSSv3 system to SP2010 Standard/Enterprise.  SItes came over but many of the lists and groups did not.  this is how we picked up the groups to have lists and libraries come over with their old assigned users...

    Export-SPWeb http://whtas30207:23805/groupsite/ -itemurl "Lists/Applications Help Desk System/" -path D:\SPMigration\ASR_manual.cmp -nofilecompression

    Note: I did not have to use the -identity switch to tell SP what site it was; also I DID NOT have to use the leading slash "/Lists/..." as described by other notes.  I also note here I DID NOT have to explicitly put the path in quotes and finally the use of the -nofilecompression switch leaves the export file in an XML format so you can re-arrange the users and groups ahead of the site structures that use them so you will NOT get the error messsage on import that SPImport can't find the group or user.

    I'm just sayin'....

  • Hi All,

    I need powershell script to get the email alert, when the site or application pool status got changed on IIS 7.0

  • Can you please some one help on this script