PowerShell Parameters Intro

Introduction to PowerShell’s Parameters

A PowerShell parameter works in a similar fashion to a DOS switch.  Introduced with a dash, -Parameters modify, clarify, and improve the underlying cmdlet.

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.

Our Vehicle to Learn About 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 Introduction
Get-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 Help
Get-Help Get-ChildItem -Full

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

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

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.

Download a free trial of Solarwinds’ Network Performance Monitor

-Path parameter

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 (.).

Required? false
Position? 1
Default value Current directory
Accept pipeline input? true (ByValue, ByPropertyName)
Accept wildcard characters? true

Parameters Modify a Cmdlet’s Behaviour

Suppose we want a list all the executables under the Windows folder?  From the -Path help section we see that it supports wildcards, so let us try *.exe.

Let me say right away there are numerous ways to achieve this goal, but I want to introduce another parameter called -Filter.

# PowerShell -File Parameter
Get-ChildItem -Path C:\Windows -Filter *.exe

Parameter Refinement: Drilldown to Sub-Directories with -Recurse

If we want Get-ChildItem to look in folders like \System32, which are underneath C:\Windows, then we need one of PowerShell’s most famous parameters -Recurse.

# PowerShell Parameter Introduction
Get-ChildItem -Path C:\Windows -Filter *.exe -Recurse #`
#| Sort-Object Name | Select-Object Name

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.

If you like this page then please share it with your friends


See more example of PowerShell’s parameters

PowerShell Tutorials  • PowerShell Parameter Introduction  • Parameter Examples of Technique

Mandatory Parameters  • PowerShell $PSDefaultParameterValues  • Top 10 PowerShell Parameters

PowerShell Parameters Index  • Parameter Hashtable Splatting  • PowerShell 3.0 Default Parameters

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.