Getting Started with PowerShell Parameters

Parameters and Cmdlets are inextricably linked, neither can do anything useful without the other. PowerShell is so intuitive that beginners may not even realize they are using parameters. At the intermediate stage scripters know to list parameters, and have a fair idea of how to use them. Advanced PowerShell users write their own functions complete with parameters.

On its own, Get-ChildItem lists the files in the current folder. The cmdlet executes in the location where your PowerShell happens to be pointing.

The problem occurs when the resulting list of files is not what you want. The solution is to append the -Path parameter, and thus persuade Get-ChildItem to focus on the folder which holds the files specified by the -Path parameter.

# PowerShell Parameter IntroductionGet-ChildItem -Path C:\Windows

Note 1: Sometimes PowerShell is so clever you don’t realise what it’s doing under the covers. The fact that you get the same result without typing the name of the parameter (-Path) can be mystifying. E.g. Get-ChildItem C: \Windows works just the same.

The answer is that if you don’t explicitly use the -Path parameter, then PowerShell assumes, by its position, that the first item after the cmdlet is the path for Get-ChildItem. How do I know all this? The answer will become a familiar cry: ‘Call for Get-Help Verb-Noun -full’, here is an example:

# PowerShell Parameter HelpClear-HostGet-Help Get-ChildItem -Full

Note 2: Clear-Host is an instruction, which I often use to delete previous results and thus reduce confusion.

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

If you scroll down through the help file’s offerings you will come to the -Path parameter. Reading its capabilities explains two factors of Get-ChildItem’s previous behaviour. This parameter operates in the first position, furthermore, if you do nothing the default is the current directory.

-Path <String[]>Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory (.).

Note 3: The basic script produces a messy output. To tidy up the list, remove the two # marks; one before the (`) backtick, the other at the start of the last line.

»

Summary of My Introduction to PowerShell’s Parameters

We can modify the behaviour of PowerShell cmdlets by appending parameters. The best way to learn about these parameters is to turn Get-Help to list a cmdlet’s parameters.

On this page we have discovered that cmdlets support multiple parameters, yet if you use no parameters there are still default values. This reinforces the idea that parameters modify a cmdlet, better still, they can take its basic action and turn it to the outcome that you really want. This is why spending time learning about parameters makes us better PowerShell scripters.