Exchange PowerShell Quickstart - Bonus
-
Twan van Beers
-
05 December, 2014
Over the past 5 episodes we have been covering all sorts of aspects of Exchange PowerShell fundamentals. This week I want to give just a few extra hints and tips as you step into the brave world of Exchange PowerShell.
Hint 1: Don’t abbreviate
Abbreviation is fine when writing directly at the command line, since no one else aside from you will ever have to read it and understand what you are doing.
$a = Get-Mailbox foreach( $b in $a ) { foreach( $c in $b.EmailAddresses ) { # Do Something } }the above is much harder to read than
$Mailboxes = Get-Mailbox foreach( $Mailbox in $Mailboxes ) { foreach( $EmailAddress in $b.EmailAddresses ) { # Do Something } }Hint 2: Use indenting in scripts
Indenting really helps understand the flow of the script, I have seen scripts that are multiple screens long and every line is a command and starts at the same point, yet there were iterations and conditions, making it very hard to follow what was going on. When I read someone else’s code that hasn’t been indented, generally the first thing I do is indent it.
$Mailboxes = Get-Mailbox foreach( $Mailbox in $Mailboxes ) { foreach( $EmailAddress in $b.EmailAddresses ) { # Do Something } }
is harder to follow than
$Mailboxes = Get-Mailbox foreach( $Mailbox in $Mailboxes ) { foreach( $EmailAddress in $b.EmailAddresses ) { # Do Something } }
Hint 3: Avoid long pipelines within scripts
Again on the command line using piping from one to another is fine, but in a script those long pipelines become hard to understand and to debug if they go wrong. In Scripts it is generally better to stick to the more traditional one task per line method.
Hint 4: Use Select-Object to bring back only what you need
This is more for scripts used in big organizations, e.g. $mailboxes = Get-Mailbox -ResultSet Unlimited works fine even in a 300,000 mailbox firm, however the amount of memory consumed runs into multiple GB. If you only need say Alias and PrimarySmtpAddress, then add a Select-Object. This will dramatically reduce the amount of memory that your PowerShell session consumes.
$Mailboxes = Get-Mailbox -ResultSet Unlimited | Select-Object Alias, PrimarySmtpAddressHint 5: Avoiding ‘Pipelines cannot be executed concurrently’
When you are using a pipeline on the command line PowerShell will by default run the next command as soon as it starts getting results back. Now depending on what the next command does that can cause issues. The end result is screeds of Red Errors saying that Pipelines cannot be executed concurrently. There are three things you can do to resolve this.
- Don’t use a pipeline
Breaking the pipeline resolves the issue since PowerShell can’t move onto the next line until it has got all the results in the previous line$TransportServers = Get-TransportServer$TransportServers | % { Get-MessageTrackingLog -Start 12/1/2014 -Server $_.Name } - Add OutBuffer
The common parameter OutBuffer can be used to set how many results need to be returned before moving to the next command (or it needs to get to the end of the result set)Get-TransportServer -OutBuffer 999 | % { Get-MessageTrackingLog -Start 12/1/2014 -Server $_.Name } - Add Sort-Object
Adding Sort-Object will block PowerShell from going forward until it has all of the results and then has sorted them all.Get-TransportServer | Sort-Object Name | % { Get-MessageTrackingLog -Start 12/1/2014 -Server $_.Name }
Hint 6: Use Group-Object to summarise data into categories
The Group-Object cmdlet summarises a result set, e.g. if we want a count of the number of mailboxes subject to each retention policy we can do:
Get-Mailbox | Group-Object RetentionPolicy | Select-object Name, CountHint 7: Use Measure-Object to summarise data numerically
Measure-Object is an easy way to get a numerical summary into sum, minimum, maximum and/or average.
Get-MailbxoStatistics -Server Server1 | Measure-Object -Property TotalItemSize -Average -Minimum -Maximum -SumHint 8: Use Out-GridView to graphically see results
The Command line is great but sometimes it is nicer to see results graphically and to be able to further filter the results.
Get-Mailbox | Out-GridViewI hope this collection of hints will help you in your foray into PowerShell mastering!
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 - Bonus">