Exchange PowerShell Quickstart - Part 2
-
Twan van Beers
-
24 October, 2014
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
| Operator | Description |
|---|---|
| -eq | Equals |
| -ne | Not equals |
| -lt | Less than |
| -gt | Greater than |
| -le | Less than or equals |
| -ge | Greater than or equals |
| -like | Matches wild card comparison |
| -notlike | Does not match wild card comparison |
| -match | Matches regular expression |
| -notmatch | Does nto match regular expression |
| -contains | Check if an array or list contains an element |
| -notcontains | Check if an array or list does not contain an element |
| -and | Logical and of two expressions |
| -or | Logical or of two expressions |
| -not | Negate 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.
Twan van Beers
Twan is a senior consultant with over 30 years of experience. He has a wide range of skills including Messaging, Active Directory, SQL, Networking and Firewalls. Twan loves to write scripts and get deep and dirty into debugging code, in order to understand and resolve the most complex of problems.
News & Insights
Trusted insights on technology and innovation to power your business growth.
-
03/06/2026
A practical engineering guide to owners, assignment, consent, credentials and ...
Read More
-
19/05/2026
Domain migrations in Microsoft 365 are rarely as simple as shifting email ...
Read More
-
12/05/2026
Over the past 10 to 15 years, Microsoft 365 has fundamentally reshaped how ...
Read More
-
07/05/2026
Yesterday we had an all company meeting and I thought it would be cool to kick ...
Read More
-
07/05/2026
Planning a tenant-to-tenant migration? Talk to us
Read More
-
31/03/2026
In tenant-to-tenant (T2T) and Google-to-Microsoft 365 migration projects, ...
Read More
-
27/03/2026
You’d think this would be easy and not an uncommon request. There are genuine ...
Read More
-
23/03/2026
Managing Google to Microsoft Migration with GAM7 and PowerShell
Read More
-
16/03/2026
When a user leaves an organisation in Microsoft 365, administrators have ...
Read MoreSubscribe to our newsletter for the latest updates and insights.
Like what you see? Stay in touch! Subscribers to our email list are among the first to receive the latest news, views and updates from Nero Blanco
Exchange PowerShell Quickstart - Part 2">