Prof. Powershell

Eureka Moments in PowerShell

Spending time to play around with PowerShell can lead to some of the best hidden secrets.

One of PowerShell's strength's over other scripting tools like VBScript, is that is easier to discover what you can do. I love discovering a command or part of a command that I didn't know about that solves a problem or adds a certain amount of elegance to my PowerShell work. Here are some tips to help you with your own Eureka moments.

First, you have to know how to use Get-Command. I often use this, especially in Powershell 3.0, to discover what is new or something I haven't tried before. I might try a command like this:

PS C:\> get-command | where {$_.verb} | group Verb | Sort name

Some commands, won't have a verb defined which is fine for now. I'll end up with something like Figure 1.

 

Invoke-WebRequest cmdlet

Figure 1. (Click image to view larger version.)

This is from a Windows 8 box with Remote Server Administration (RSAT) involved so you might get different results. But now I can get an idea of what commands are available to me. Or I might try creating a hash table from this.

PS C:\> $cmdhash = get-command | where {$_.verb} | group Verb –AsHashTable

Now, each verb is a hashtable property and the keys are the associated commands.

PS C:\> $cmdhash.out

CommandType Name ModuleName

----------- ---- ----------

Function Out-Xaml ShowUI

Cmdlet Out-Clipboard Pscx

Cmdlet Out-Default Microsoft.PowerShell.Core

Cmdlet Out-File Microsoft.PowerShell.Utility

Cmdlet Out-GridView Microsoft.PowerShell.Utility

Cmdlet Out-Host Microsoft.PowerShell.Core

Cmdlet Out-Null Microsoft.PowerShell.Core

Cmdlet Out-Printer Microsoft.PowerShell.Utility

Cmdlet Out-String Microsoft.PowerShell.Utility

The next step to discovery is using help. I'd like to think this is self-evident but many people forget to look for it. Take the time to read complete help.

PS C:\> help out-gridview –full

I often find nuggets of PowerShell goodness, especially in v3 where a cmdlet may have been revised and expanded. Or I can turn back to Get-Command to check out parameters.

PS C:\> (get-command out-gridview).Parameters.keys

InputObject

Title

Wait

OutputMode

PassThru

Verbose

Debug

ErrorAction

WarningAction

ErrorVariable

WarningVariable

OutVariable

OutBuffer

This is handy also for discovering parameter aliases which may or not be well documented. Here's an example:

PS Scripts:\> (get-command invoke-cimmethod).parameters.Values | Select Name,Aliases,ParameterSets | Out-Gridview

 

Invoke-WebRequest cmdlet

Figure 2. (Click image to view larger version.)

As you can see in Figure 2 the MethodName parameter has an alias of Name which you wouldn't know simply by reading the cmdlet help.

Take some time to dig around in PowerShell. You might be surprised what you come across and I'd love to hear about your Eureka moments.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular