PowerShell: How to display Exchange/AD command results in sorted by Property Names?


I am extremely disappointed with Microsoft PowerShell Modules for Exchange, Active Directory, Azure and others that the results of (e.g., Get-ADUser, Get-Mailbox) commands are not sorted by properties. At times I was looking for specific property value that I don’t remember top of my head. I listed all available properties and search for the property.

e.g., Get-Mailbox <username> | Format-List

If you see the results of the above command, Property and values display at random order. Searching for a property that you don’t remember becomes unnecessarily harder.

So I figured out how to display the results in sorted by property name. You may pipe your command results to this command.

| Select-Object * | Out-String -Stream | Sort-Object

Use this command like this:

Get-Mailbox <username> | Select-Object * | Out-String -Stream | Sort-Object

Here is what I did. Select-Object * selects all the properties, Out-String -Stream converts the results to string and lastly Sort-Object sorts the string results.

My Life got little better.

I didn’t stop there I created a function and added the function to PowerShell Profile, so it will loaded every time. Here is the function:

function Sort-Results { Param([Parameter(ValueFromPipeline=$true)] $ResultObject) ; $ResultObject | Select-Object * | Out-String -Stream | Sort-Object }

Use this function like this:

Get-Mailbox <username> | Sort-Results

Feel free to use this function. If you like this idea, leave me a comment below.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s