PowerShell Parameters

All Windows PowerShell cmdlets and functions supply parameters.  By mastering the seemingly endless options for these ‘switches’, you will gain precision when scripting with the underlying cmdlet.

Parameter names are introduced by a hyphen (-), and followed by a text or numeric value, for example:

Cmdlet (or function) -Parameter value

Get-WmiObject -Class Win32_ComputerSystem

Make the Most of PowerShell’s Parameters

This section explains how to make PowerShell’s parameters your friends, and thus write better scripts.

 ♦

Example of a PowerShell Parameter from the GCI Cmdlet

Parameter Example: -File as in Get-ChildItem -File

# One of Get-ChildItem’s parameters
………………………………………
-File [<SwitchParameter>]
Gets files.

To get only files, use the File parameter and omit the Directory parameter.
Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

Note 1: Experiment with Get-ChildItem -Directory; see the difference, compared with plain Get-ChildItem.

Get-ChildItem -Directory C:\Windows

See more on getting started with PowerShell parameters »

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

Techniques to Research PowerShell’s Parameters

Get-Help Unfamiliar-Cmdlet -Full
My first method to investigate a cmdlet and its parameters is Get-Help.  In the above example -Full is itself a parameter!  Incidentally, I prefer -Full to -Parameter or -Detailed.

# Example of Researching PowerShell’s Parameters
Clear-Host
Get-Help Get-ChildItem -Full

PowerShell Function ParametersNote 2: Microsoft provide another useful source of information with:
Get-Help about_Parameters

Required or Mandatory Parameters

Many of PowerShell’s cmdlets churn out results without us having to explicitly add a parameter.

# Example PowerShell’s Default Parameter
Clear-Host
Get-Service

The logic behind the list you get from plain ‘Get-Service is worth studying:

  1. The Get-Service cmdlet expects a parameter called -Name in the first position.
  2. The default for -Name is ‘All services’.
  3. My point is that PowerShell is easy to learn.  On its own, without any parameters, this cmdlet delivers a list of services.

However, a significant number of cmdlets won’t function until you supply a value for their required parameter, for example Get-Eventlog.  See more on PowerShell’s mandatory parameters.

SwitchParameter

If you study a parameter’s details carefully, you will see that many are <string[]>, yet a few parameters are <SwitchParameter>.

Here is an example of SwitchParameter taken from Get-ChildItem:
-ReadOnly <SwitchParameter>.  This simply means that this parameter’s default is ‘False’.  Merely using a parameter of type SwitchParameter reverses the default and sets it to true.

# Example of PowerShell SwitchParameter
Clear-Host
Write-Host "Total of all files in root "(Get-ChildItem c:\).count
Write-Host "Read only files in root"(Get-ChildItem c:\ -ReadOnly).count

Note 3: Avoid overthink.  Just remember that by default SwitchParameter is set to false.  When you need to turn-on this type of parameter just type its name.  There is no need to set a value of any kind, not even $true.

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset now!

Download your fully functional trial copy of the Engineer’s Toolset v10

Measuring PowerShell -Filter

Whenever I employ the Where-Object cmdlet I ask myself, could I get better performance using -Filter instead?  To compare the two techniques I use Measure-Object.

#Measuring a PowerShell -Filter
Clear-Host
Measure-Command{
Get-ChildItem ${env:ProgramFiles(x86)} -Filter *.exe -Recurse }
# Result Milliseconds: 1103

See more on PowerShell -Filter parameter »

A Nifty Method to Change a Parameter’s Default Actions

You can have fun with a built-in PowerShell command which modifies the default actions of cmdlets, this example sets the default value of -Path to C: \Windows\System 32.

# Change Default Values of Get-ChildItem
$PSDefaultParameterValues=@{
"Get-ChildItem:path"="C:\Windows\System32\";
}
Get-ChildItem

Creating Windows PowerShell Functions

Use normal PowerShell command(s) + Param (Parameters) to create a function. Function.  I see a Parameter doing for PowerShell what a -switch does for a DOS command.  In both cases the user gets options for the basic command.

More Windows PowerShell Sections

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.