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.

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.

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.

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

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

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

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

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

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.

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

µ

1 Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: