PowerShell Parameter Techniques

Techniques for PowerShell Parameters

On this page I want to show you methods for researching PowerShell parameters.

 ♦

Exploit a Cmdlet’s Help File

Each PowerShell cmdlet (or function) has it’s own help file, which is a rich source of parameter information.  Access could not easier, just precede the cmdlet with help.  However, there is one trick, always append -Full.  The benefit is that you get examples of the cmdlet and its parameters in action.

# PowerShell Parameter Help
Get-Help Get-WmiObject

Note 1: On its own, this instruction gives only the names of the parameters.

Much better to append -Full, and see how to use the parameters.

# PowerShell Parameter Help
Clear-Host
Get-Help Get-WmiObject -Full

-Class <String>
Specifies the name of a WMI class. When this parameter is used, the cmdlet retrieves instances of the WMI class.

Required? true
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false

Note: Even Get-Help has help!  For instance researching Get-Help reveals that you could substitute -Detailed or -Examples for -Full.

Find Cmdlets that Use a Particular Parameter

Remoting is popular in PowerShell 2.0 and later, but which cmdlets support ComputerName?

# Find PowerShell Cmdlets with a ComputerName Parameter
Clear-Host
Get-Command | Where-Object { $_.parameters.keys -Match "Computer"}

Note 2:  The key properties are revealed by: .parameters.keys.

Note 3: I prefer the match comparator, but you could try:
Where-Object { $_.parameters.keys -Contains "ComputerName"

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

Named PowerShell Parameters

The more parameters that you employ in a PowerShell script, the greater the need to name them.

Take as an example, Get-Eventlog system,
This instruction works because even though ‘system’ is not a named parameter, it is in position 1, which is reserved for -LogName. 

Take another example, here we wish to truncate the log output by introducing -Newest 50; this parameter must be named as there are at least 3 parameters that could be in the second position.

# PowerShell Named Parameters
Clear-Host
Get-EventLog system -Newest 50 -Message "*media*" -EntryType Error

List Positional PowerShell Parameters

Here is a trick script which makes use of ExpandedProperties to discover which cmdlets have positional parameters.

# PowerShell Position Parameter List
Clear-Host
$a = Get-Command New*
Foreach ($cmdlet in $a) {
$Cmdlet.ParameterSets | Select-Object -ExpandProperty parameters | `
Where {$_.Position -gt "0" } | FT $Cmdlet.Name, Name, Position -auto
}

Note 4: This is a script to give you ideas for further research.  For example you broaden the search for cmdlets by changing the value of $a = Get-Command from New* to Get*.

Note 5: I also use a similar script to find Mandatory or Required parameters  See more on Mandatory PowerShell parameters »

Summary of PowerShell Parameter Introduction

The idea is that parameters modify a cmdlet, they can take it’s basic action and turn it to the outcome that you really want.  The best way to learn about these capabilities is to employ Get-Help to list a cmdlet’s parameters.

Time spend learning about parameters subtleties will make us better PowerShell scripters.

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

 


See more Microsoft PowerShell tutorials

PowerShell Tutorials  • Methods  • Cmdlets  • PS Snapin  • Profile.ps1  • Exchange 2007

Command & Expression Mode  • PowerShell pipeline (|)  • PowerShell ‘where‘  • PowerShell ‘Sort’

Windows PowerShell Modules  • Import-Module  • PowerShell Module Directory 

If you see an error of any kind, do let me know.  Please report any factual mistakes, grammatical errors or broken links.