Fishing for PowerShell: Leveraging Get-Command and Show-Command

Posted on August 19, 2015 by Jeff Hicks in PowerShell with 0 Comments

During my recent summer vacation, I spent some time fishing with my son, who’s an avid fisherman and takes it much more seriously than I do. As we meandered around the lake on the boat, I started thinking that fishing is as much hunting for the right spot or some clue as to where the fish might be. Granted, knowing where to look for these clues takes a lot of experience. And because I’m never truly on vacation, I started thinking about how this might apply to someone learning PowerShell. If you want to know where the fish are, you need to know where to look. Fortunately, PowerShell has a few spots where you can find answers, and I’m happy to share them.

PowerShell’s Get-Command

The first place you should stop is the Get-Command cmdlet. Not sure what command to run to get event logs? Take advantage of Get-Command and its support for wildcards. I’m not saying you will get what you need on the first attempt.

You might try this:

If you try that, then you’ll see a lot of results. Don’t give up, let’s try again.

Using PowerShell's Get-Command cmdlet. (Image Credit: Jeff Hicks)

Using PowerShell’s Get-Command cmdlet. (Image Credit: Jeff Hicks)

Your results might vary depending on what modules you have installed. You can also limit your search by getting commands by noun.

Using nouns with Get-Command. (Image Credit: Jeff Hicks)

Using nouns with Get-Command. (Image Credit: Jeff Hicks)

You can also use wildcards to search for different nouns. Likewise, you can get commands that use a specific verb.

Limiting our Get-Command search to verbs. (Image Credit: Jeff Hicks)

Limiting our Get-Command search to verbs. (Image Credit: Jeff Hicks)

If you notice from the screenshots or your own use of Get-Command, you’ll see that it finds all types of executables. You can limit the search to a specific type.

And perhaps my favorite use of Get-Command is to see what commands are packed away inside a module or snapin.

Using Get-Command with a module. (Image Credit: Jeff Hicks)

Using Get-Command with a module. (Image Credit: Jeff Hicks)

You can also use the same parameters that I showed you earlier to fine tune the results.

Fine tuning our command. (Image Credit: Jeff Hicks)

Fine tuning our command. (Image Credit: Jeff Hicks)

You can also use this with snap-ins, even with the –module parameter. Make sure to add the snap-in first.

Using PowerShell's Get-Command with a snap-in. (Image Credit: Jeff Hicks)

Using PowerShell’s Get-Command with a snap-in. (Image Credit: Jeff Hicks)

Windows PowerShell’s Show-Command

Related to Get-Command is Show-Command. Where the former will tell you where a command comes from, the latter will help you figure out how to use it. You have probably seen this in the PowerShell ISE, which I’ll talk about in a moment. Let’s say you’ve looked at help for a cmdlet like Get-VMSnapshot:

Get-VMSnapshot. (Image Credit: Jeff Hicks)

Get-VMSnapshot. (Image Credit: Jeff Hicks)

Let’s run Show-Command:

You should get a pop-up like this:

Get-VMSnapshot pop-up. (Image Credit: Jeff Hicks)

Get-VMSnapshot pop-up. (Image Credit: Jeff Hicks)

Each of the tabs, e.g., VMName, corresponds to a different parameter set. You can click on the different tabs to see the different parameter options. Parameter names with an asterisk are required. I also like the drop-down menus for some parameters. I can go ahead and fill out the form.

If I click Copy, the final command will be copied to the Windows clipboard. In this case, I will end up with this command:

If I click Run, PowerShell executes the command and closes the dialog box.

Executing our newly copied command. (Image Credit: Jeff Hicks)

Executing our newly copied command. (Image Credit: Jeff Hicks)

If you run Show-Command, you’ll notice that you won’t get your prompt back until the dialog box is dismissed.

Sponsored

Using Show-Command in the PowerShell ISE

In the PowerShell ISE, the Show-Command panel is displayed by default. If you’ve closed it, you can re-open it from the menu

The Show Command Add-On in the PowerShell ISE. (Image Credit: Jeff Hicks)

The Show Command Add-On in the PowerShell ISE. (Image Credit: Jeff Hicks)

Here, the display is a bit different in that you can search for a cmdlet from a given module.

You can also start typing a command name.

Typing a command in the PowerShell ISE. (Image Credit: Jeff Hicks)

Typing a command in the PowerShell ISE. (Image Credit: Jeff Hicks)

In the ISE, you will see this message if the module has not been loaded:

Go ahead and click “Show Details” to import the module. In the regular console, PowerShell will automatically import the module. Now I have a form I can fill out.

Importing the module in the PowerShell ISE. (Image Credit: Jeff Hicks)

Importing the module in the PowerShell ISE. (Image Credit: Jeff Hicks)

Here’s the tricky part. If I click Run, PowerShell will execute the finished command in the PowerShell ISE. If I click Insert, PowerShell will insert it into the command window.

I can also click Copy and then paste it anywhere I want, including the script pane in the ISE.

Here’s my full command:

Sponsored

Personally, I’d love for an Insert command that lets you place the command into the script pane. It would also be handy if there’s an option to use splatting. But regardless, the Show-Command tool makes it easy to discover just what you need. I have a few more PowerShell fishing tips, which I’ll save for next time.

Sponsored

Tagged with