Powershell

PowerShell ValidateSet: Choosing From a List

The attribute “ValidateSet” in PowerShell is utilized to validate the supplied values or objects. Meaning that it only allows the values entered from the supplied list of items. The specified parameter can also be utilized in the PowerShell functions. During the runtime, it checks whether the list contains the specific values or not. If the value is found in the list, then this cmdlet will continue to run, else it will throw an error.

This article will provide insightful details about the PowerShell “ValidateSet” attribute.

PowerShell ValidateSet: Choosing From a List

The attribute “Validate” in PowerShell is utilized to check and validate whether the entered value or object exists in the supplied objects or not. Examples demonstrating the stated attribute are provided below.

Example 1: Create a Function to Prompt the User to Enter a Value and Validate it Using the “ValidateSet” Cmdlet

The following example will validate the value entered by the user:

Function OS {
}
[ValidateSet('Windows','iOS','Linux')]
$read = read-host -prompt 'Enter the OS name'

 
According to the above code:

    • First, create a function named “OS”.
    • Then, outside the function, create a “ValidateSet” argument and supply the stated values.
    • After that, initialize a variable and assign the “read-host” cmdlet.
    • Then, add the “-prompt” parameter and specify the text to be displayed at the time of taking input from the user:

 

Let’s validate the given value whether it exists in the collection or not:

Windows

 

Now, let’s enter the object value that does not exist in the supplied set of objects:

Manjaro

 

It can be seen that the console throws an error.

Example 2: Use the “Param” Statement Along With the “ValidateSet” Attribute to Validate the Given Value

The stated example will prompt the user to enter the value and then validate the entered value:

Param(
     [Parameter(Mandatory)]
      [ValidateSet("Adam","John","Bill","Katty")]
     $Employees_Info
 )
$employees_age = [ordered]@{
     'Adam' = 28
     'John'   = 26
     'Bill'   = 32
 }
 $Employees_Info | Foreach-Object {
     $age_output = "{0} is {1} years old." -f $_, $employees_age[$_]
     Write-Output $age_output
 }

 
In the above-stated code:

    • First, create a “Param()” statement and make it mandatory.
    • Then, pass the “ValidateSet” attribute with the set of objects inside it and also pass the hash table assigned “$Employees_info” variable.
    • After that, create a hash table. In that hash table assign the values to the objects.
    • Then, write the hash table assigned variable and add the “|” pipeline.
    • After that, add the “Foreach-Object” cmdlet to display the output to the PowerShell console:

 

Let’s enter the value that is supplied to the collection of objects to validate it:

Bill

 

It can be observed that the command executed successfully because the entered object exists in the supplied objects.

Now, let’s enter the object which does not exist in the collection of objects:

James

 

It can be seen, that the console throws an error because the entered object does not exist in the supplied objects collection.

Conclusion

The cmdlet “ValidateSet” in PowerShell specifies the set of possible values for a cmdlet parameter argument. Moreover, this can also be used by the PowerShell function. When it is enabled, it accepts and validates the given value. If the value exists in the collection, then the program will further execute. Else it won’t execute and will throw an error. This blog has provided detailed information about the “ValidateSet” attribute.

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.