PowerShell Productivity Hacks: How I use Get-Command

If you’ve been using PowerShell for very long at all, then you should already be familiar with Get-Command and Get-Help. While I like to say that Get-Command is for finding commands and Get-Help is for learning how to use those commands once you’ve found them, there is overlap between these two commands depending on how you use them.

I believe in following the best practice of not using aliases or positional parameters in any code that I save or share with others, but in this blog article, I’m going to show how things work in the real world. I will provide the full cmdlet and parameter names and then show what I really type since the code is not shared or saved at that point in time.

Who really wants to read pages and pages of help information for a command when much of it is irrelevant to what you’re trying to accomplish? Once I’ve found a PowerShell command to accomplish the task at hand, I use Get-Command with the Syntax parameter to quickly determine how to use it.

Get-Command -Name Get-NetAdapter -Syntax
gcm Get-NetAdapter -Syntax

gcm-syntax-param1a.jpg

All of the syntax that’s returned means something. Square brackets means optional unless it’s two square brackets together and that means it accepts more than one value.

I can see that Get-NetAdapter has three parameter sets.

gcm-syntax-param3a.jpg

In the first parameter set, I can tell that the Name parameter isn’t mandatory because the parameter and the type of value it accepts are both completely enclosed in square brackets.

gcm-syntax-param4a.jpg

Within those, I can tell that the Name parameter is positional because it’s surrounded by its own set of square brackets.

gcm-syntax-param5a.jpg

When the Name parameter is used positionally, it must be in the first position since it’s listed first.

gcm-syntax-param6a.jpg

The Name parameter accepts a datatype of string because that’s what follows the name of the parameter.

gcm-syntax-param7a.jpg

Also, it accepts an array of strings because two square brackets follow the datatype for that particular parameter.

gcm-syntax-param8a.jpg

Notice that switch parameters such as IncludeHidden don’t have a datatype after them.

gcm-syntax-param9a.jpg

If a switch parameter is specified, it’s on or true and if it’s not specified, it’s off or false. Depending on what’s being done, a switch parameter such as Confirm might be on by default. To turn it off, you can specify -Confirm:$false without the quotes. Also, to use a switch parameter in a hash table when splatting, set the value using the Boolean $true or $false. Example: Confirm = $false.

If for some reason, I need to know more about a particular parameter, I’ll view the help for that particular parameter instead of all of the full help for the entire command.

help Get-NetAdapter -Parameter Name

gcm-syntax-param2a.jpg

Thoughts, questions, comments? Please post them as a comment to this blog article.

ยต