Musings of a PC

Thoughts about Windows, TV and technology in general

Sorting help from PowerShell

I thought I’d start by sharing a question I had, and the answer I received from kind contributors to the Microsoft newsgroup microsoft.public.windows.powershell. If you haven’t started looking at this newsgroup, I would certainly recommend it. Even if things go over your head initially, it is useful to try to read at least some of the postings in order to try to see what other people are doing with PS.
 
So: what was my question?
 
Well, PowerShell has a very powerful help system. During my learning period, I’m relying heavily on it to help me try to figure out how to achieve what I want. The help system supports wildcards and there is a strong naming convention in PS. You can combine both features to help you guess at what you are looking for.
 
For example, if you want to get something, the likelihood is that the command starts with Get-. So what commands are there?
 
Name                       Category                   Synopsis
—-                       ——–                   ——–
Get-Command                Command                    Retrieves basic inform…
Get-Help                   Command                    Opens the help files
Get-History                Command                    Gets a listing of the …
Get-PSSnapin               Command                    Lists registered PSSna…
Get-EventLog               Command                    Gets eventlog data for…
Get-ChildItem              Command                    Retrieves the child it…
Get-Content                Command                    The get-content comman…
Get-ItemProperty           Command                    Retrieves the properti…
Get-WmiObject              Command                    Produce a WMI Object o…
Get-Location               Command                    Displays the current l…
Get-PSDrive                Command                    Gets the drive informa…
Get-Item                   Command                    Returns an object that…
Get-PSProvider             Command                    Gets information for t…
Get-Process                Command                    Gets a list of process…
Get-Service                Command                    Gets a list of services.
Get-Acl                    Command                    Gets the access contro…
Get-PfxCertificate         Command                    Gets the pfx certifica…
Get-Credential             Command                    Gets a credential obje…
Get-ExecutionPolicy        Command                    Gets the effective exe…
Get-AuthenticodeSignature  Command                    Gets the signature obj…
Get-Alias                  Command                    Returns alias names fo…
Get-Culture                Command                    Gets the culture infor…
Get-Date                   Command                    Gets current date and …
Get-Host                   Command                    Gets host information
Get-Member                 Command                    This Cmdlet enumerates…
Get-UICulture              Command                    Gets the uiculture inf…
Get-Unique                 Command                    Gets the unique items …
Get-Variable               Command                    Gets a Ps variable
Get-TraceSource            Command                    Lists properties for g…
 
OK – but that list isn’t in alphabetical order. How about piping it through sort?
 
help get-* | sort
Name                       Category                   Synopsis
—-                       ——–                   ——–
Get-Command                Command                    Retrieves basic inform…
Get-Help                   Command                    Opens the help files
Get-History                Command                    Gets a listing of the …
Get-PSSnapin               Command                    Lists registered PSSna…
Get-EventLog               Command                    Gets eventlog data for…
Get-ChildItem              Command                    Retrieves the child it…
Get-Content                Command                    The get-content comman…
 
… and so on. Well, that didn’t help, so what is going on here? Well, it turns out that the help command is actually a built-in function that does this:
 
Get-Help $help | Out-Host -paging
 
It takes the parameter you provide, passes it to Get-Help and then pipes it through to the paging system. This means that if you then try to pipe it to sort, sort doesn’t see anything useful to sort on.
 
The solution, therefore, is to do this:
 
get-help get-* | sort -property name | Out-Host -paging
 
One of my next challenges will be to see how to create a get-help-sorted cmdlet with an alias.
Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: