Verbs and Nouns
Every built-in PowerShell cmdlet is composed of a verb and noun (Verb-Noun)—for example, Get-SPSite.
The verb part of a cmdlet indicates an operation that will be
performed, and the noun indicates the object on which the operation will
be performed. The Get-SPSite command gets a SPSite object (SharePoint site collection object).
The other common PowerShell operations (verbs) are Set- (modifies object), Remove- (deletes object), and New- (creates a new object).
Most
SharePoint cmdlets come with these four verb-noun combinations.
However, some objects might have more or less cmdlets depending on the
object type—for example, the SPSite object also comes with Backup-, Move-, and Restore- cmdlets.
Working with Variables, Function, and Cmdlets
You can use PowerShell
variables to store results from any executed command. The following
example demonstrates how to use variables:
#$SiteCollection = Get-SPSite "http://portal.companyabc.com"
#$SiteCollection.Url
#$SiteCollection.Owner
The preceding given command saves site collection (from http://portal.companyabc.com) to a variable $SiteCollection; the variable can then be used to retrieve properties. The preceding example shows how to retrieve Url and Owner properties and display them in the shell.
The value stored to $SiteCollection is a SharePoint-specific .NET Framework object: SPSite and exposes all available properties as in any other .NET language.
To learn more about SharePoint 2010 SDK, see the following URL:
http://msdn.microsoft.com/en-us/library/ee557253(office.14).aspx
PowerShell Command Piping
In real life, people use
pipes to transfer goods (for example, oil) from one end to another.
PowerShell pipes are similar because they enable you to easily
transform, modify, or pass the result of one cmdlet to another.
Let’s say we want to list all site collections in a SharePoint farm. Get-SPSite cmdlet displays all the site collections, but in large farms it might returns hundreds or thousands of results. If Get-SPSite is combined with a filter command (over pipe), it will return only specific site collections that require attention.
Get-SPSite | Where {$_.Url –eq "http://portal.companyabc.com"}
By using the pipe “|”, we see results (all site collections) of the Get-SPSite cmdlet that are passed to the Where pipe, which dictates that only site collections whose URL equals to http://portal.companyabc.com will be released to the next cmdlet in pipeline.
The preceding example has only one pipe, but additional pipes could be used:
Get-SPSite | Where {$_.Url –like "*portal*"} | Sort RootWeb | Select RootWeb, Url
The preceding command lists all the sites collections that contain the word portal in their URL and then sorts them by RootWeb property and displays the results in table format with two columns RootWeb and Url.
Figure 5 shows the results of running the preceding code in the SharePoint 2010 Management Shell.
Formatting Results
Results retrieved from PowerShell cmdlets are usually displayed in a tabular form but can be further customized by adding the format command. Typical formatFormat-List, Format-Table, and Format-Wide. commands are
Get-SPSite | Select RootWeb, Url, Owner | Format-Table
The preceding command will list all the site collections and display results in table format. (We selected RootWeb, Url, and Owner properties of site collection to be displayed in the table.)
In addition to formatting
results, you can choose a different output destination than a default
one (shell screen). One of the useful output destinations is GridView:
Get-SPSolution | Select * | Out-GridView
The preceding command displays all installed SharePoint solutions in a GridView format, as shown in Figure 6. If you do not have installed solutions, you could combine Out-GridView with other cmdlets, such as Get-SPSite.
PowerShell—The New STSADM
With the release of SharePoint
2010, PowerShell becomes the most important tool for SharePoint
administration automation. Every administration operation is first
delivered for PowerShell, and some operations (for example, advanced
customization during installation, multitenancy
administration, and so on) are available via PowerShell only. The
built-in cmdlets have much more options than Central Administration UI
and STSADM commands.
Administrators can still
combine all three interfaces; an administrator that only periodically
needs to create a single site collection should probably still use
Central Administration, but PowerShell should be the first choice when
it comes to administration and automation.