TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
How To Create Power Shell Function Mandatory Parameter Value
1. Windows PowerShell Basics – Function Mandatory 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.........................................................................................................................................2
Invoke Function – Attribute Name ...........................................................................................................2
PowerShell Output #1...........................................................................................................................2
Invoke Function – Without Passing Argument Value ...............................................................................2
Invoke Function – Passing Argument Value Space ...................................................................................3
PowerShell Output #2...........................................................................................................................3
Invoke Function – Passing Argument Value..............................................................................................3
PowerShell Output #3...........................................................................................................................3
Invoke Function – Passing Argument Value..............................................................................................3
PowerShell Output #4...........................................................................................................................3
2. Windows PowerShell Basics – Function Mandatory 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. When the value is not given,
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 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, “Parameter” is set to true and for the argument position 1, which will ensure that the
argument value is mandatory.
#
# Simple Function with one Mandatory Argument Value
#
function MandatoryParameter
{
param(
[Parameter(Mandatory=$True, Position=1)]
[ValidateNotNullOrEmpty()]
3. Windows PowerShell Basics – Function Mandatory Argument
2 | P a g e
[string]$Parameter_Position_One
)
Clear-Host
Write-Host "`nParameter Value that was passed : $Parameter_Position_One`n"
[console]::beep(500,300)
}
Invoke Function
To invoke / call the function, call the function from the PowerShell CLI or PowerShell ISE.
MandatoryParameter 007
Invoke Function – Attribute Name
Invoke the function and pass value for the specific attribute.
MandatoryParameter -Parameter_Position_One 007
PowerShell Output #1
When the function is called / executed with parameter value being passed; below output will be displayed.
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;
4. Windows PowerShell Basics – Function Mandatory Argument
3 | P a g e
Invoke Function – Passing Argument Value Space
When you intend to set the argument value with multiple words, enclose value with double quotes after
invoking the function.
PowerShell Output #2
Invoke Function – Passing Argument Value
When you intend to run the script is run with single quote;
MandatoryParameter 'Function Parameter Passing Mandatory Argument Value'
PowerShell Output #3
Invoke Function – Passing Argument Value
When you intend to run the script along with the against each parameter name its value;
MandatoryParameter –Parameter_Position_One Parameter
PowerShell Output #4