DevEX - reference for building teams, processes, and platforms
How To Create PowerShell Function Mandatory Parameter and Optional Parameter
1. Windows PowerShell Basics – Function Mandatory and
Optional Argument
i | P a g e
Table of Contents
Overview.......................................................................................................................................................1
Applies To..................................................................................................................................................1
Pre-Requisites ...........................................................................................................................................1
PowerShell Script – Function ........................................................................................................................1
Code Snippet.............................................................................................................................................1
Invoke Function – Attribute Name ...........................................................................................................2
PowerShell Output #1...........................................................................................................................2
Invoke Function – Without Passing Argument Value ...............................................................................3
PowerShell Output #2...........................................................................................................................3
2. Windows PowerShell Basics – Function Mandatory and
Optional Argument
1 | P a g e
Overview
PowerShell functions are helpful in saving time when you have repetitive task(s) to be executed. In this
guide we will demonstrate, passing function argument(s) value as mandatory and an optional argument.
When the value is not given for the mandatory, function will prompt for parameter value.
Applies To
Tested on Windows 10, Windows 2008 R2 and Windows 2012.
Pre-Requisites
Launch PowerShell Command Console or PowerShell ISE.
To run this script, Execution Policy should be set to either of these “AllSigned” or “RemoteSigned” or
“Unrestricted”, you can get current execution policy by running the command; “Get-ExecutionPolicy”.
Each Policy type and its purpose is shown in the below table.
Policy Type Purpose
Restricted No scripts can be run. Windows PowerShell can be used only in interactive mode.
AllSigned Only scripts signed by a trusted publisher can be run.
RemoteSigned Downloaded scripts must be signed by a trusted publisher before they can be run.
Unrestricted No restrictions; all Windows PowerShell scripts can be run.
PowerShell Script – Function
This PowerShell script call function with function’s mandatory argument / parameter value, when the
argument value is passed; Passed value will be displayed else an exception will be thrown.
Code Snippet
In this function, one “Parameter” is set to false and “Parameter” is set to true; for argument position 1 is
optional and position 2 argument is mandatory.
#
# Simple Function With One Argument Mandatory Value and Second Argument Optional
#
function MandatoryParameter_And_OptionalParameter
{
param(
3. Windows PowerShell Basics – Function Mandatory and
Optional Argument
2 | P a g e
[Parameter(Mandatory=$False, Position=1)]
[string]$Parameter_Position_One,
[Parameter(Mandatory=$True, Position=2)]
[ValidateNotNull()]
[string]$Parameter_Position_Two
)
Clear-Host
Write-Host "`n1st Parameter Value that was passed : $Parameter_Position_One`n"
Write-Host "`n2nd Parameter Value that was passed : $Parameter_Position_Two`n"
[console]::beep(500, 300)
}
Invoke Function – Attribute Name
To invoke / call the function, call the function from the PowerShell CLI or PowerShell ISE. Invoke the
function and pass value for the specific attribute.
MandatoryParameter_And_OptionalParameter -Parameter_Position_Two Value_Passed
PowerShell Output #1
After execution of the function and mandatory argument value is passed; argument value is displayed as
below;
4. Windows PowerShell Basics – Function Mandatory and
Optional Argument
3 | P a g e
Invoke Function – Without Passing Argument Value
When the parameter/argument value is not passed; script will ask for input of argument value, as shown
below;
MandatoryParameter_And_OptionalParameter
PowerShell Output #2