PowerShell ValidateSet

The Powershell validate set can be used to validate the powershell parameters that are getting supplied to the script. This is useful, when you want to validate a boolean value like True or False, or when you want to ensure that, user is selecting the right choice, when supplying the input values to a function or a script.

Function Sample-Validation {
          [Parameter(Position=0,Mandatory=$True, HelpMessage="Select the choice, True or False")]
          [ValidateSet("True","False" )]
          [string]$Choice = "False",
# Sample Choices
Sample-Validation -Choice "True"
Sample-Validation -Choice "False"
Sample-Validation -Choice "Falseeeee"
Sample-Validation -Choice $null
Sample-Validation -Choice ""

When the choice is True

When the choice is False

When the choice is having spelling mistake

When the choice is null

When the choice is empty

Similare write ups can be found in the Microsoft dev blogs here

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.