PowerShell Mandatory Parameters

Investigating PowerShell Parameters

On this page I will explain parameter concepts, which are useful to the intermediate PowerShell script writer.  For example, mandatory (required) and named parameters.


Mandatory PowerShell Parameters

A good way to understand PowerShell’s need for a mandatory, or required parameter is to launch Get-Eventlog.

# PowerShell Mandatory Parameters

Get-EventLog cmdlet
Get-EventLog at command pipeline position 1
Supply values for the following parameters:

The problem with plain Get-Eventlog is that the cmdlet halts because it does not know whether you want the system, security, application or some other log.  Setting aside marketing speak, this weakness is actually a strength, Get-Eventlog works with all sorts of logs, all you need to do is supply the LogName!  For example: Get-Eventlog -LogName system.

In terms of learning more about PowerShell, the question is how do you know which parameters are mandatory or ‘Required’?  The answer is to run Get-Help against the cmdlet, and then study the parameters’ descriptions.

# PowerShell Required Parameters
Get-Help Get-Eventlog -full

-LogName <String>
Specifies the event log. Enter the log name of one event log. Wildcard characters are not permitted.
This parameter is required.

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

Note 1: This parameter is in position number 1.  Thus if you just type a name such as ‘system’, or ‘application’, then PowerShell will be able to interpret this word as a value for -LogName.

My point: This instruction works just fine: Get-EventLog System
For a quick look at the logs you don’t need to explicitly name the parameter in position 1: Get-EventLog -LogName System.

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

Research PowerShell’s Mandatory Parameters

Here is script to detect cmdlets containing parameters that are required; the key clause is: Where IsMandatory -eq "True".

# PowerShell Mandatory Parameter Script
$a = Get-Command -Verb Get
Foreach ($cmdlet in $a) {
$Cmdlet.ParameterSets | Select-Object -ExpandProperty parameters | `
Where-Object {$_.IsMandatory -eq "True" } `
| Format-Table $Cmdlet.Name, Name, IsMandatory -auto

Note 2: On closer inspection, my script finds only cmdlets using the Get verb; however, I am sure that you could modify my example to find all cmdlets containing mandatory parameters.

Named PowerShell Parameters

The more parameters that you employ in a PowerShell script, the greater the need to name them.  Indeed, all parameters are named by default, the only time you can get away with omitting the name is if it’s a positional parameter; a fact you check by looking in the helpfile for parameters with: ‘Position? 1’.

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
Get-EventLog system -Newest 50 -Message "*media*" -EntryType Error

Note 3: -Newest, -Message and -EntryType are named parameters; whereas system is a value for the required -LogName parameter.

Summary of PowerShell Mandatory Parameters

Understanding mandatory parameters explains why cmdlets sometimes work when you append an intuitive value; for example Get-Eventlog system.  Appreciating the nuance of named parameters show the way forward if you want to improve your scripts by adding more parameters.

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.