Understanding the pipeline
The single most important concept in PowerShell is the use of its flexible, object-based pipeline. You may have used pipelines in Unix-based shells, or when working with the cmd.exe
command prompt. The concept of pipelines is similar to that of sending the output from one command to another. But, instead of passing plain text, PowerShell works with objects, and we can accomplish some very complex tasks in just a single line of code. In this recipe, you'll learn how to use pipelines to string together multiple commands and build powerful one-liners.
How to do it...
The following pipeline command would set the office location for every mailbox in the DB1
database:
Get-Mailbox -Database DB1 | Set-Mailbox -Office Headquarters
How it works...
In a pipeline, you separate a series of commands using the pipe (|
) character. In the previous example, the Get-Mailbox
cmdlet returns a collection of mailbox objects. Each mailbox object contains several properties that contain information such as the name of the mailbox, the location of the associated user account in Active Directory, and more. The Set-Mailbox
cmdlet is designed to accept input from the Get-Mailbox
cmdlet in a pipeline, and with one simple command we can pass along an entire collection of mailboxes that can be modified in one operation.
You can also pipe output to filtering commands, such as the Where-Object
cmdlet. In this example, the command retrieves only the mailboxes with a MaxSendSize
equal to 10
megabytes:
Get-Mailbox | Where-Object{$_.MaxSendSize -eq 10mb}
The code that the Where-Object
cmdlet uses to perform the filtering is enclosed in curly braces ({}
). This is called a script block, and the code within this script block is evaluated for each object that comes across the pipeline. If the result of the expression is evaluated as true, the object is returned; otherwise, it is ignored. In this example, we access the MaxSendSize
property of each mailbox using the $_
object, which is an automatic variable that refers to the current object in the pipeline. We use the equals (-eq
) comparison operator to check that the MaxSendSize
property of each mailbox is equal to 10 megabytes. If so, only those mailboxes are returned by the command.
Tip
Comparison operators allow you to compare results and find values that match a pattern. For a complete list of comparison operators, run Get-Helpabout_Comparison_Operators
.
When running this command, which can also be referred to as a one-liner, each mailbox object is processed one at a time using stream processing. This means that as soon as a match is found, the mailbox information is displayed on the screen. Without this behavior, you would have to wait for every mailbox to be found before seeing any results. This may not matter if you are working in a very small environment, but without this functionality in a large organization with tens of thousands of mailboxes, you would have to wait a long time for the entire result set to be collected and returned.
One other interesting thing to note about the comparison being done inside our Where-Object
filter is the use of the mb
multiplier suffix. PowerShell natively supports these multipliers and they make it a lot easier for us to work with large numbers. In this example, we've used 10mb
, which is the equivalent of entering the value in bytes because behind the scenes, PowerShell is doing the math for us by replacing this value with 1024
*1024
*10
. PowerShell provides support for the following multipliers: kb
, mb
, gb
, tb
, and pb
.
There's more...
You can use advanced pipelining techniques to send objects across the pipeline to other cmdlets that do not support direct pipeline input. For example, the following one-liner adds a list of users to a group:
Get-User | Where-Object{$_.title -eq "Exchange Admin"} | Foreach-Object{ Add-RoleGroupMember -Identity "Organization Management" ` -Member $_.name }
This pipeline command starts off with a simple filter that returns only the users that have their title
set to "Exchange Admin"
. The output from that command is then piped to the ForEach-Object
cmdlet that processes each object in the collection. Similar to the Where-Object
cmdlet, the ForEach-Object
cmdlet processes each item from the pipeline using a script block. Instead of filtering, this time we are running a command for each user object returned in the collection and adding them to the "Organization Management"
role group.
Using aliases in pipelines can be helpful because it reduces the number of characters you need to type. Take a look at the following command where the previous command is modified to use aliases:
Get-User | ?{$_.title -eq "Exchange Admin"} | %{ Add-RoleGroupMember -Identity "Organization Management" ` -Member $_.name }
Notice the use of the question mark (?
) and the percent sign (%
) characters. The ?
character is an alias for the Where-Object
cmdlet, and the %
character is an alias for the ForEach-Object
cmdlet. These cmdlets are used heavily, and you'll often see them used with these aliases because it makes the commands easier to type.
Tip
You can use the Get-Alias
cmdlet to find all of the aliases currently defined in your shell session and the New-Alias
cmdlet to create custom aliases.
The Where-Object
and ForEach-Object
cmdlets have additional aliases. Here's another way you could run the previous command:
Get-User | where{$_.title -eq "Exchange Admin"} | foreach{ Add-RoleGroupMember -Identity "Organization Management" ` -Member $_.name }
Use aliases when you're working interactively in the shell to speed up your work and keep your commands concise. You may want to consider using the full cmdlet names in production scripts to avoid confusing others who may read your code.
See also
The Looping through items recipe
The Creating custom objects recipe
The Dealing with concurrent pipelines in remote PowerShell recipe in Chapter 2, Exchange Management Shell Common Tasks