Finally, let’s take a look at the cmdlets
used to manage sites in SharePoint 2010. All the cmdlets for managing
sites in SharePoint 2010 use the SPWeb noun.
PS > Get-Command -Noun SPWeb
CommandType Name Definition
----------- ---- ----------
Cmdlet Export-SPWeb Export-SPWeb [-Identity] <SPWebPipeBi
Cmdlet Get-SPWeb Get-SPWeb [[-Identity] <SPWebPipeBind
Cmdlet Import-SPWeb Import-SPWeb [-Identity] <SPWebPipeBi
Cmdlet New-SPWeb New-SPWeb [-Url] <String> [-Language
Cmdlet Remove-SPWeb Remove-SPWeb [-Identity] <SPWebPipeBi
Cmdlet Set-SPWeb Set-SPWeb [-Identity] <SPWebPipeBind>
Let’s look at how to use each of these, starting with New-SPWeb.
Creating Sites in SharePoint 2010
The New-SPWeb cmdlet creates a new site in
an existing site collection. The only parameter that is required is the
URL, which must be in an existing site collection and unique. You can
also specify the language, site template, name, and description, as well
as set unique permissions, add the site to the parent site’s Quick
Launch bar, and set the top navigation bar options.
Here’s an example of creating a new site:
PS > New-SPWeb -Url http://SPServer01/NewSite -Template "STS#0" '
>> -Name "New Site" -Description "My New Site" -AddToTopNav -UseParentTopNav
Url
---
http://spserver01/NewSite
In this example, we set the template to use. Next, we specify the name and description of the site. Finally, we use the AddToTopNav switch parameter to add the site to the top-level navigation bar and the UseParentTopNav switch parameter to specify that the site uses the same top-level navigation bar as the parent site.
Note
If you do not set a template when creating the new site, you can add it later, either in the browser or with the Set-SPWeb cmdlet.
Configuring Sites in SharePoint 2010
The Set-SPWeb cmdlet lets you configure existing sites in SharePoint 2010. The following example changes the description of an existing site.
PS > Get-SPWeb http://SPServer01/NewSite | Select-Object -Property Description
Description
-----------
My New Site
PS > Get-SPWeb http://SPServer01/NewSite |
>> Set-SPWeb -Description "A New Description"
PS > Get-SPWeb http://SPServer01/NewSite | Select-Object -Property Description
Description
-----------
A New Description
First, we use Get-SPWeb and pipe the object to the Select-Object cmdlet to retrieve the current value of the Description property. We then use the Set-SPWeb
cmdlet to change the site’s description. Finally, we verify that the
change occurred by retrieving the site’s description again.
Exporting and Importing Sites in SharePoint 2010
With the Export-SPWeb cmdlet, you can export a site, as follows:
PS > Export-SPWeb -Identity http://SPServer01/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 site, such as
lists, document libraries, and even list items. You use the ItemUrl
parameter to export lists or list items from a site. Here is an example
of exporting a list called Calendar from a site:
PS > Export-SPWeb -Identity http://SPServer01/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.
Once you have an export file, you can use the Import-SPWeb
cmdlet to import it into a site. Importing a site works as long as you
specify a site collection that contains a matching template; otherwise,
an error occurs.
In the following example, we will delete the Calendar list in SharePoint 2010 and perform an import with the Import-SPWeb cmdlet. Figure 1 shows how to delete a list in SharePoint 2010.
Now that the list is removed, we can go ahead and run Import-SPWeb:
PS > Import-SPWeb -Identity http://SPServer01/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.
Removing Sites in SharePoint 2010
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://SPServer01/NewSite -Confirm:$false