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 7.5 shows the results of running the preceding code in the SharePoint 2010 Management Shell.

Figure 7.5. PowerShell returns a list of site collections with a word portal in the URL.

image

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset