Working with Webs
Site collections contain webs, or
SPWebs if you are speaking about SharePoint PowerShell commands. Like
the other main objects in the SharePoint hierarchy, you can list, add,
modify, and remove webs. Administrators tend to spend a lot of time
working with webs because they are so numerous and this is where end
users actually do their work. The SPWeb
object contains many items that end users work with, such as lists and
libraries. Now is probably a good time to let you know that there are
no cmdlets to access objects below the SPWeb object. This means that there are no commands for lists, libraries, or files, to name a few objects, below the SPWeb
object. However, that does not mean you cannot access them via
PowerShell — just that you will not find cmdlets specific to these
objects. You are free to access these objects via the object model.
Listing all the webs of the farm is slightly different from listing SPWebApplications or SPSites. The Get-SPWeb
cmdlet requires at least one parameter. It will not list all SPWebs on
the farm if you omit the parameters; there’s just too many SPWebs in a
farm for that to make sense. On the plus side, you are allowed
wildcards, regular expressions (with the use of the –RegEx switch parameter), and filters (with a script block), similar to Get-SPSite. The -Identity parameter will also accept a relative path if the -Site parameter is used.
Let’s look at a few ways to list SPWebs, starting with a single web. To access a single SPWeb object, use the -Identity parameter, passing in the URL, as demonstrated in Figure 13.
The next example returns all the SPWebs in the
farm, including any Central Administration webs, sorted by URL and
displaying only the Title and URL:
Get-SPWebApplication –IncludeCentralAdministration | Get-SPSite |
Get-SPWeb |
Sort-Object Url | Format-Table Title, Url
Figure 14
displays the results of the attempt to display all the SPWebs on the
farm. As discussed earlier, the cmdlet will limit the number of objects
returned to 20 for performance reasons. If the number of objects is
greater than 20, a warning will be displayed. To display all the
SPWebs, you need to add the -Limit property set to All for both Get-SPSite and Get-SPWeb:
Get-SPWebApplication –IncludeCentralAdministration | Get-SPSite –Limit All |
Get-SPWeb –Limit All | Sort-Object Url| Format-List Title, Url
Creating a new web is similar to creating new site collections. You use the New-SPWeb command and a host of parameters to define the new SPWeb. One of these parameters, -Url, is required, but the rest are optional, such as -Name and -Template. The following command creates a new SPWeb based on the Team Site template. Once the web has been created, the SPWeb object is returned and displayed on the screen:
New-SPSWeb –Url http://portal.contoso.com/teams/IT/SP2013, -Template "STS#1"
–Name "SP 2013 Implementation"
You cannot back up or restore an individual web;
that is reserved for the site collection and farm. You can work around
this limitation, however. SharePoint has content deployment
functionality that enables you to automate the process of copying
content from one farm to another. It does this at the SPWeb level.
PowerShell has two cmdlets, Export-SPWeb and Import-SPWeb,
that are built on that framework. While you can’t back up and restore
webs, we can export them and import them. Be careful — these exports
are not full fidelity, as their focus is on content. They won’t contain
non-content pieces such as alerts, workflows, or permissions.
You just created a new web, so go ahead and export it. Use the Export-SPWeb command, passing in the -Identity and -Path parameters. The -Path parameter indicates where the exported web file will be placed, and it must include both a filename and the path:
Export-SPWeb http://portal.contoso.com/ops -Path c:\ExportWeb\opsExport.cmp
You can import the web into an existing or new web. The Import-SPWeb
command requires the identity of the web to import to and the path to
the exported web file. The command to import your exported site to a
new web is as follows:
Import-SPWeb http://portal.contoso.com/DemoImport -Path c:\ExportWeb\opsExport.cmp
Finally, you can remove your web by using the Remove-SPWeb command. This command is similar to the Remove-SPSite
command. It removes one web at a time, prompting users for confirmation
along the way. You can pipe an unlimited number of SPWebs into the remove command. Don’t forget the earlier discussion of the -WhatIf parameter. As shown in Figure 15, use the -WhatIf parameter to see what would happen if you ran this command:
Get-SPSite | Get-SPWeb –Limit All | Remove-SPWeb –WhatIf
As you can see, the pipeline is very powerful. Be careful when you pipe objects into destructive commands. The -WhatIf parameter and PowerShell’s confirm message will help to keep you out of trouble — that is, if you pay attention.