Mandatory Parameters in PowerShell

Mandatory Parameters in PowerShell

Using Mandatory Attribute

Use the Mandatory attribute to make parameters mandatory in PowerShell.

First, we defined a function named DisplayName preceded by the function keyword. Inside this function, we used the param keyword (also referred to as param() block), which was used to define the $Name parameter; this parameter was of the string data type.

The param() block also contained the [Parameter()] attribute representing the $Name parameter is mandatory and can not be omitted; it is because we set the Mandatory attribute to $true. In the above code, the declaration of the $Name parameter has the following characteristics:

  • It takes input from the PowerShell console.
  • It is a mandatory (required) parameter.
  • It takes one string type value.

Next, we used Write-Host cmdlet to print the value of the $Name parameter as you can see in the above output. Finally, we invoked the DisplayName method by providing "John" as an argument to the function.

Remember, there is a clear difference between a parameter and an argument; the parameter is the variable’s name, while an argument is a value contained by that variable. So in the above example, $Name is the parameter while "John" is the argument.

What will happen if we call the DisplayName function without providing the value for the $Name parameter? Let’s do that by using the following example.

This time, we got the above-mentioned error because we called the DisplayName function but didn’t provide the value for the $Name parameter, which was required.

Now, think as a program user and do unexpected things, such as pass an integer type value as demonstrated in the following code snippet and observe the results.

The above code was executed successfully, but it should not work. Why? The code ran because we provided a mandatory argument stored in the $Name parameter, and its data type was decided to string within the param() block. So, everything after that ran without producing any error, but we want this code not to run if anything except alphabetical letters is provided. Remember, numbers can’t be the names.

It means we must have some validation to avoid semantic errors (these are the problems with the code, which does not produce any error but does not perform the right things). See the following section to learn how to validate and accept only string type values.

Use the Mandatory with the ValidatePattern

Use the Mandatory attribute with the ValidatePattern attribute to define a regex to validate the provided argument.

Here, we called the DisplayName function twice; for the first call, we passed an integer value 123 as an argument, and for the second call, we specified a string type value, "John". Unfortunately, for the first call, the above code displayed an error saying it could not validate the argument on the specified parameter. In contrast, it worked fine for the second function call. Why?

It is because we used the [ValidatePattern()] attribute to validate the input value. This attribute took a regular expression used to validate the provided argument. In the above case, we used ^[a-zA-Z]+$ regex, which matched one or multiple upper and lower case letters. Here, + matched one or more letters; the ^ and $ were used to ensure that the specified pattern matched the whole string, not a substring.

Depending on the requirements, we can use other validate patterns as well, such as ValidateCount, ValidateLength, and ValidateSet, etc. that you can learn here. Now, there is also a possibility when we want to accept the mandatory argument from a given set of values. In that case, we can use the ValidateSet attribute. Let’s learn it in the following section.

Use the Mandatory with the ValidateSet

The Mandatory attribute is used with the ValidateSet attribute to validate the provided argument.

Here, we validated the pattern and checked if the provided argument belonged to the defined set. For that, we used the [ValidateSet()] to mention the valid values for a parameter argument which means the above program will only work if it will get "Mary" or "John" as an argument; otherwise, it will generate an error saying it can not validate the parameter. See the above outputs for all three function calls.

Until this point, we have learned via specifying string values and numbers, but what if we provide an empty string ("") or $null value for the Mandatory parameter; in that case, the [ValidateSet()] and [ValidatePattern()] attributes will generate an error if we’ll pass an empty string or $null value as an argument.

Why? It is because ValidateSet will check if the argument belongs to the set. At the same time, ValidatePattern will try to match the input argument, and both will generate an error if anything goes wrong.

Now, if we do not use any of these attributes, neither ValidateSet nor ValidatePattern, how can we detect an empty string or $null value for a Mandatory argument? Let’s see how we can do that in the following section.

Use the Mandatory with the ValidateNotNullOrEmpty

Use the Mandatory attribute with the ValidateNotNullOrEmpty attribute to validate if the provided argument is neither an empty string nor a $null value.

We got an error for the second and third function call because of the ValidateNotNullOrEmpty() attribute, which was used to identify an empty string and $null.

The above code will not show any error if you specify null or "null" instead of $null because it will treat null as a string type value. So, we must use the ValidatePattern attribute to handle this situation, as demonstrated below.

We got an error for the second and fifth function calls because the empty string and the $null variable do not match the provided regular expression. In contrast, the third and fourth function calls displayed an error because the above code accepted all alphabetical combinations, excluding the null value.

Using Multiple Mandatory Attributes

Use the n number of Mandatory attributes to make n parameters mandatory in PowerShell.

To have n parameters mandatory, we need to use [Parameter(Mandatory=$true)] before declaring the parameter’s name, as demonstrated in the above example. Another situation can be having one mandatory parameter and one optional. In that case, we need to omit the Mandatory attribute for the parameter we want to make optional. See the following example.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *