. Advertisement .
..3..
. Advertisement .
..4..
If you don’t know how to get Powershell command line arguments, don’t panic. This guide will walk you through all the basics.
You can manage arguments utilizing the function param() from Windows Powershell. This parameter function is a necessary part of every script. Developers enable input from script users at runtime through the usage of parameters.
How to get Powershell command line arguments
Define Powershell Script Parameters
You can define parameters for functions and scripts with the param() function. This function may have more than one variable that defines parameters.
Hello_World.ps1:
param(
$message
)
Still, to make sure the parameter only receives the appropriate kind of input, assign the data type to a parameter utilizing the [Parameter()] parameter block and enclose this data type with [] before variables.
Hello_World.ps1:
param(
[Parameter()]
[String]$message
)
In the above Hello_World.ps1 sample, the message variable only takes in the passed value when the supplied value comes with a String data type.
Powershell Script Named Parameters
Named parameters are a term used to describe one technique to utilize parameter function in the script. The name of a variable is used as the entire parameter name when a function or script is called using named parameters.
A Hello World.ps1 was made in this sample, and the variables were defined within a parameter function. Keep in mind that you can include more than one variable in a parameter function.
Hello_World.ps1:
param(
[Parameter()]
[String]$message,
[String]$emotion
)
Write-Output $message
Write-Output "I am $emotion"
You may also utilize named parameters as a type of argument when running a file .ps1.
.\Hello_World.ps1 -message 'Hello World!' -emotion 'happy'
Output:
Hello World!
I am happy
Set Default Values For Powershell Script Parameters
The value of an argument may be pre-assigned by providing this parameter a value inside the script. Next, executing your script without passing the values from the execution line takes the variable’s default value which is defined in the script.
param(
[Parameter()]
[String]$message = "Hello World",
[String]$emotion = "happy"
)
Write-Output $message
Write-Output "I am $emotion"
.\Hello_World.ps1
Output:
Hello World!
I am happy
Switch Powershell Script Parameters
The switch parameter specified by the data type [switch] is another parameter you may use. It is utilized for Boolean or binary values to signify false or true.
Hello_World.ps1:
param(
[Parameter()]
[String]$message,
[String]$emotion,
[Switch]$display
)
if($display){
Write-Output $message
Write-Output "I am $emotion"
}else{
Write-Output "User denied confirmation."
}
Observe how we call the script with a script parameter utilizing the following syntax.
.\Hello_World.ps1 -message 'Hello World!' -emotion 'happy' -display:$false
Output:
User denied confirmation.
Mandatory Powershell Script Parameters
It’s typical for a script to require more than one parameter in order to run. By including an attribute Mandatory within the [Parameter()] parameter block, we may make a mandatory parameter.
Hello_World.ps1:
param(
[Parameter(Mandatory)]
[String]$message,
[String]$emotion,
[Switch]$display
)
if($display){
Write-Output $message
Write-Output "I am $emotion"
}else{
Write-Output "User denied confirmation."
}
Windows PowerShell prevents the script from running and prompting you for a value while running Hello_World.ps1.
.\Hello_World.ps1
Output:
cmdlet hello_world.ps1 at command pipeline position 1
Supply values for the following parameters:
message:
The Bottom Line
Now you know all the basics of getting Powershell command line arguments. At first, this task could appear difficult, but once you grasp the fundamentals, you will find it quite simple.Bonus: Why not learn more Powershell skills with us? Let’s start with guides on utilizing where-object and get the Windows version.
Leave a comment