Listing Unique Values and Sorting them using PowerShell

I always like to dig deeper into the various products I am using. Today, I wanted to get my hands dirty and start playing with PowerShell for SQL Server. One of the things I wanted to do was to get a figure out how to quickly produce a list of all the various Modules that were currently loaded in one of my PowerShell session. If you ever get started with PowerShell for SQL Server, you’ll soon realize how easy it is to get lost in all the various modules that are made available to you to interact with the database server.

We all know the good old Get-Command PowerShell cmdlet that retruns a complete list of all cmdlets that are currently available in a PowerShell session. You can quickly extend this and only return the list of module names from which each cmdlet belongs to. Something like:

Get-Command | Select ModuleName

Will still return an extensive list, but will ensure it only contains the Modules’ name. What we really want is to get a list of unique values. The Select operation in PowerShell offers a switch called -Unique that will do just that. Then all that is left is to have the list sorted in alphabetical order using the Sort operation and we are done. So my complete PowerShell command will look like the following:

Get-Command | Select ModuleName – Unique | Sort ModuleName


Leave a Reply

Your email address will not be published. Required fields are marked *