Powershell

How do I Make Parameters Mandatory in PowerShell?

Parameters are very useful in taking input from the user in PowerShell. They are typically utilized in functions and scripts. The parameters are enclosed within the parenthesis of the “param()” block. However, recently, PowerShell has introduced a “Parameter()” method, where the “Mandatory” attribute value is passed inside the “Parameter()” method.

This write-up will cover the aspects to make the parameters mandatory.

How to Make Parameters Mandatory in PowerShell?

The PowerShell attribute “[Parameter()]” is utilized to add special behaviors such as Position, Help Message, or Mandatory. More specifically, a mandatory parameter is used to make the parameters mandatory.

The parameter in PowerShell can be made mandatory by adding the “Mandatory=$true” attribute to the parameter description. If you want to make the parameter optional, leave the “Mandatory” statement empty.

Example 1: Passing Mandatory Parameters in PowerShell

In this example, we will make parameters mandatory in PowerShell:

function test(){

param(

  [Parameter(Mandatory=$true)]

  [string]$Name,

  [Parameter(Mandatory=$true)]

  [string]$Profession)

  "$Name and $Profession"

}

test John Doctor

According to the above code:

  • First, create a function and add the “param()” block inside it.
  • Each parameter inside the “param()” block is associated with the “[Parameter()]” method.
  • Inside the “[Parameter()]” method, the “Mandatory” attribute value is assigned, and it is set to “$True”, which means it is enabled to take the value from the user.
  • Outside the function, the function name is written, which is “test”. The two arguments to be passed inside the parameter are “John” and “Doctor”:

It can be observed from the output that the values have been successfully passed to the mandatory parameter.

Example 2: Not Passing Any Mandatory Parameters in PowerShell

Let’s test the function by not passing the mandatory parameter’s value to it when it is enabled:

> test John

As you can see, the script returned an error because the value was not passed to the mandatory parameter.

Example 3: Leaving Mandatory Parameter Optional in PowerShell

In this example, let’s leave the mandatory parameter optional. To do so, leave the “[Parameter()]” attribute out, as it is demonstrated below:

That was all about making parameters mandatory in PowerShell.

Conclusion

The parameters can be made mandatory by adding the “[Parameter()]” method inside the “param()” method. Within this method, add the “Mandatory” attribute value and assign the “$True” value to it in order to enable it. This write-up guided about making the parameters mandatory in PowerShell.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.