Exchange PowerShell Quickstart – Part 2

Exchange PowerShell Quickstart – Part 2

Last week in part 1 we looked at the background on PowerShell, explored terminology and some basic commands. This week we are going to see how we combine commands together on the command line via ‘the pipeline’

The Pipeline

The pipeline is just a single line of PowerShell with one or more | (pipe character) separating cmdlets. This allows the results from the cmdlet on the left of the pipe to be used as input for the next command. So the results flow from left to right like oil going through a pipeline. e.g. Get-MailboxDatabase DB1 | Get-Mailbox will return all the mailboxes in database DB1. Several additional concepts are often used on the pipeline.

1. Curly brackets

Curly brackets {} are used to group cmdlets together for the purpose of filtering or iteration

2. Round brackets

Round breackets () are used to group cmdlets together for the purpose of evaluation

3. where-object, where or ?

These are used to filter the results before passing them to the next cmdlet.

4. foreach-object, foreach or %

These are used to iterate through the results and then do something with each object found

5. $_

$_ is used to refer to the results of the previous cmdlet on the pipeline

Phew that was a lot to add but we need to understand those in order to develop more complex pipelines

Get all mailboxes from mailbox databases where the mailbox database name contains the letters US
Get-MailboxDatabase | where { $_.Name -like ‘*US*’ } | Get-Mailbox

Get all of the emails out of the tracking logs sent from someone to someoneelse
Get-TransportServer *US* | foreach { Get-MessageTrackingLog -Server $_.Name -Sender someone@contoso.com -Recipients someoneelse@contoso.com }

Get a count of all of the mailboxes in mailbox database DB1
(Get-MailboxDatabase DB1 | Get-Mailbox ).count

Get all of the open non shadow copy queues that have more than 5 messages
Get-TransportServer *US* | Get-Queue | ? { $_.MessageCount -gt 5 -and $_.DeliveryType -notlike ‘Shadow*’ }

Within the examples above you will see things like -gt, -and, -notlike. These are Powershell’s inbuilt operators. Powershell is case insensitive by default but you can prepend i or c to each of the comparison operators to explicitly use case insensitive (i) or case sensitive (c) comparisons. You can use Get-Help about_Comparison_Operators or Get-Help About_Logical_Operators to see them all but here is a list of the commonly used ones

OperatorDescription
-eqEquals
-neNot equals
-ltLess than
-gtGreater than
-leLess than or equals
-geGreater than or equals
-likeMatches wild card comparison
-notlikeDoes not match wild card comparison
-matchMatches regular expression
-notmatchDoes nto match regular expression
-containsCheck if an array or list contains an element
-notcontainsCheck if an array or list does not contain an element
-andLogical and of two expressions
-orLogical or of two expressions
-notNegate an expression

The hardest for someone new to programming to understand is how the $_ one works. As a cmdlet passes answers from left to right, the $_ can be used to inspect those results. You can think of $_ like an Excel spreadsheet with column headings, e.g. columnA, columnB and columnC. Now within your Powershell you can refer to those columns as $_.ColumnA, $_.ColumnB and $_.ColumnC. This way when you add a where or foreach into the pipeline PowerShell knows that you want to inspect or iterate through all of the rows and look at the value of a practicular column as it goes.

This leads us nicely onto next week’s topic which is to look at how these results are actually structured technically and then looking at variables.

Related posts: