268 ❘ CHAPTER 10 admiNisteriNg sharePoiNt 2010 With WiNdoWs PoWershell the section “Disposing of SharePoint Variables” at the end of this chapter. Notice that we are calling the Get-SPManagedAccount command to retrieve an SPManagedAccount object, which is required for the AppPool account. You can see what managed accounts you have by using the Get-SPManagedAccount command with no parameter, as shown in Figure 10-22. FIGURE 1022 Now that you know how to create a web application using the SharePoint commands, it would be useful to learn how to remove an SPWeb application. It probably comes as no surprise that to remove a web application, you use the Remove-SPWebApplication command. This command requires you to select a specific web application, which prevents the deletion of multiple applications at once. Figure 10-23 shows how to remove the web application you just created. FIGURE 1023 Notice how PowerShell is smart enough to prompt you before it destroys your precious information. Yes, there is a way to “override” this helpful prompt, but we will leave that as an exercise for read- ers who want to learn enough about PowerShell to change this setting and take responsibility for the consequences. We don’t need any midnight phone calls when you accidentally delete your web application. Working with Site Collections The site collection level is where life gets interesting, for a number of reasons. First, since the 2003 version of SharePoint there has been a disconnect between the way the site collections are referred to in the Administration UI and object model. Next, the objects that you will be working with in this section require the proper disposal; otherwise your application might just start to hiccup, or worse. Using SharePoint Commands ❘ 269 Let’s start by clarifying that a site collection is represented in the SharePoint object model as an SPSite. That should be enough clarifi cation for the purposes of this section. We have looked at site collections in various chapters of this book already. The site collection belongs to one and only one Web application. The site collection is generally defi ned as a boundary for items such as content types and permissions. Like other objects in SharePoint 2010, you can list, create, modify, and remove a site collection or SPSite using the SharePoint PowerShell commands. You can also back up and restore a site collection using PowerShell. The SPSiteAdministration commands Get-SPSiteAdministration and Set- SPSiteAdministration allow administrators who do not have access to the site collection to manage certain elements of it. Use of the SPSite-based commands assumes some amount of access to the site collection. Let’s start by listing all site collections on the farm. There are two common methods to do this. If you do not need to list the Central Administration site collection (see the section “Working with Web Applications”), you can use the Get-SPSite command as described earlier in this chapter. As usual, the default formatting for the SPSite object provides only a few items for display. Figure 10-24 shows the Get-SPSite command in action. FIGURE 1024 The Get-SPSite command, as well as other Get commands in SharePoint 2010, will return only 20 items before providing a warning that the list was limited. These commands limit the number of objects returned for performance reasons. You will greatly appreciate the limit functionality of the Get commands the fi rst time someone tries to return hundreds of sites and webs. The Get-SPSite command has an optional Limit parameter that can be set to a number or to All if you wish to return all items. You will see how to use the Limit parameter in the following section. If you want to include site collections associated with Central Administration, you need to start with the Get-SPWebApplication command with the IncludeCentralAdministration parameter. You then need to pipe the results to the Get-SPSite command, as shown in Figure 10-25. 270 ❘ CHAPTER 10 admiNisteriNg sharePoiNt 2010 With WiNdoWs PoWershell FIGURE 1025 Of course, sometimes you need to get a single site collection or possibly a smaller subset of site col- lections. Using SharePoint 2010, along with some basic PowerShell commands, you have several ways to do just that. To get a specific site collection, you can use the Identity parameter. Like the Identity parameter used with the Get-SPWebApplication command, you can provide the command with a few differ- ent values; and like the Get-SPWebApplication command, the actual word Identity is generally not seen. In this case, the Identity parameter can use wildcards as well as regular expressions (when used in conjunction with the RegEx switch parameter). To get a single site collection, simply pass in the Url as a parameter to the Get-SPSite command, which is piped to the Format-List command as demonstrated in Figure 10-26. Feel free to throw an asterisk into the mix to see what you get back. FIGURE 1026 Using SharePoint Commands ❘ 271 The Get-SPSite command also has an optional Filter parameter that will perform server-site filtering of site collections, which provides a faster way to limit the SPSites returned. The Filter parameter will limit the results of the Get-SPSite command using a script block. A script block is simply a block of script enclosed by brackets. The Filter parameter can be used to filter on Owner, Secondary Owner, and LockState. Figure 10-27 shows the use of the Filter parameter and the script block. The $_ represents the current object in the pipeline. FIGURE 1027 Don’t forget about using the pipeline to filter your site collections. The Filter parameter will only filter on Owner, Secondary Owner, or LockState. What happens if you need to select on other properties? If you can’t seem to get from here to there using any of the preceding methods, you can always reach for the Where-Object command. The Where-Object command is a PowerShell command that uses a script block to filter objects. You commonly use the $_ reference to the current pipeline object to check a property and decide whether to keep the object or ignore it. Use Get-Help Where-Object to learn more about the Where-Object command. The following example reaches out a little further and retrieves only those site collections that have a single SPWeb object, which is the root web: Get-SPSite –Limit All |Where-Object {$_.allwebs.Count –eq 1} Before removing any site collections, let’s look at the backup and restore options. It’s a good practice to back up the site in case you realize you really needed it after removing it. By now you should be able to guess that the command to back up a site is Backup-SPSite. The site collection backup requires that you identify the site to back up and the path, including the filename, to save the backup image. This command by itself will back up only a single site collection. For example, to back up the site.Contoso.com/teams/IT site collection, you would use the following command: Backup-SPSite http://site.contoso.com/teams/IT -path c:\backups\contoso\it\it.bak While that’s handy, it doesn’t scale very well. Part of the “power” of PowerShell is its ability to loop through objects. Earlier in this chapter you saw how easy it is to get a list of all of your site collec- tions. You also know how to back one up. Therefore, you may be thinking that you should be able to combine those two tasks in order to back up all of your site collections. If so, your instincts are correct. PowerShell provides exactly that capability. Behold, the “back up all of your site collections in a single script” script: Get-SPWebApplication | Get-SPSite | ForEach-Object{$FilePath = “c:\backups\” + $_.Url.Replace(“http://”,”“).Replace(“/”,”-”) + “.bak” ; Backup-SPSite –Identity $_.Url -Path $FilePath} 272 ❘ CHAPTER 10 admiNisteriNg sharePoiNt 2010 With WiNdoWs PoWershell There’s a lot going on there, but when it’s broken down it’s easy to understand. The first two cmd- lets get the list of web applications and site collections in the farm. The next part walks through the list of site collections and for each one creates a variable named $FilePath that consists of C:\ backups\ plus the name of the site collection, with the protocol (http://) removed and any slashes in the URL replaced with dashes. Finally, you use your old friend Backup-SPSite to back up the current site collection to the location you just built with $FilePath. So simple, yet so powerful. You’ve probably already figured out that you would use the Restore-SPSite command to restore the backup. The Restore-SPSite command requires the usual standard Identity and Path param- eter. To restore the it.bak file, use the following command: Restore-SPSite http://site.contoso.com/teams/IT -path c:\backups\contoso\it\it.bak Now that you have a backup and know how to restore the site collection, it is time to finally remove the site collection. To do that, you use the Remove-SPSite command. Like other destructive commands, you will be prompted for each site collection you want to delete. Although the Remove-SPSite will remove only one site collection, you are free to pass the SPSite object into the Remove-SPSite command using the PowerShell pipeline. You can now appreciate the fact that PowerShell prompts you to allow the dele- tion of each and every site collection. Now is also a good time to talk about PowerShell’s WhatIf parameter. Well-behaved cmdlets that are potentially destructive in nature support the optional WhatIf switch parameter, and the Remove- SPSite cmdlet is indeed one of those cmdlets. If you add the WhatIf switch parameter to the Remove-SPSite command, the command will not actually remove the site but instead indicate what will happen if you remove the WhatIf parameter. Nice touch, isn’t it? It might not make sense if you are working with a single site but imagine if you ran this command: $WebApps = Get-SPWebApplication -IncludeCentralAdministration Now you have a variable that contains all Web applications. Suppose you later decide to remove all site collections. That is easy enough: $WebApps | Get-SPSite | Remove-SPSite. That is fine until you realize, too late, that you just deleted the Central Administration site collection. With the WhatIf parameter, you are forewarned about the pain you are about to inflict on yourself. Figure 10-28 shows you how a smart administrator can leave work on time. Consider using the WhatIf parameter whenever you use a command that might destroy your data. It will help avert those frantic late-night restores and a fair amount of swearing. FIGURE 1028 Using SharePoint Commands ❘ 273 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 commands 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. 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 10-29. FIGURE 1029 Now use the Filter parameter to filter your SPWebs on the server side, which performs better than returning all SPWebs for further filtering. The Filter parameter can be used with the Template and Title properties of the SPWeb. Remember to use a script block with a Filter parameter. The follow- ing command returns a list of SPWebs that are based on the Blank Team site: Get-SPSite | Get-SPWeb –Filter {$_.Template –eq “STS#1”} 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 10-30 displays the results of the attempt to display all of 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 of 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 274 ❘ CHAPTER 10 admiNisteriNg sharePoiNt 2010 With WiNdoWs PoWershell FIGURE 1030 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, Urlis required; but the rest are optional, such as Name and Template. The following command creates a new SPWeb based on the Team Site template: New-SPSWeb –Url http://site.contoso.com/teams/IT/SP2010, -Template “STS#1” –Name “SP 2010 Implementation” Once the web has been created, the SPWeb object is returned and displayed on the screen. You cannot back up or restore an individual web; that is reserved for the site collection and farm. What you can do is export and import a web. Because we just created a new web, let’s go ahead and export it. To export the web, 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. Use the Export-SPWeb command to export your new web: Export-SPWeb http://corpNet.contoso.com/ops -Path c:\ExportWeb\opsExport.cmp You can import the web into an existing or new web. The Import-Web command requires the iden- tity 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://corpNet.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 10-31, use the WhatIf parameter to see what would happen if you ran this command: Get-SPSite |Get-SPWeb –Limit All |Remove-SPWeb –WhatIf Using SharePoint Commands ❘ 275 FIGURE 1031 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! Working with Objects below the Web Level As mentioned earlier, SharePoint PowerShell commands generally work from the SPWeb object and above. That means you will not find commands such as Get-SPList or New-SPLibrary in the out- of-the-box commands. That does not mean that there is no way to access these and other items not exposed by the included commands. It means that we need to start thinking more like developers and attack the object model. This is exactly how we worked with SharePoint 2007 and PowerShell when we had no SharePoint cmdlets. We had to walk uphill both ways to and from school, and we liked it! This section does not delve too far into this subject, but it looks at how you can use PowerShell to list a web’s lists and libraries, and add and then remove a SharePoint list. That will provide you with the foundation to move outside the commands supplied by SharePoint 2010. Lists and libraries are children object of the SPWeb object we just looked at. The SPWeb object con- tains a single property, Lists, which is a collection of all its lists and libraries. To retrieve all the . admiNisteriNg sharePoiNt 2010 With WiNdoWs PoWershell FIGURE 1025 Of course, sometimes you need to get a single site collection or possibly a smaller subset of site col- lections. Using SharePoint 2010, . content types and permissions. Like other objects in SharePoint 2010, you can list, create, modify, and remove a site collection or SPSite using the SharePoint PowerShell commands. You can also back. 268 ❘ CHAPTER 10 admiNisteriNg sharePoiNt 2010 With WiNdoWs PoWershell the section “Disposing of SharePoint Variables” at the end of this chapter. Notice that we