- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use the ValidateSet Attribute in PowerShell function?
The ValidateSet attribute in PowerShell function is to validate the values entered from the set which means, it allows only specific values from the set. To understand better consider the below example, we have an array and we need to check if entered values are among array or not then we will use the below code.
Function PetAnimalsCheck { param( [String]$Animal ) $Animals = "Cow","Dog","Cat","Horse","Camel","Elephant" if($Animals -contains $Animal) { Write-Output "Animal is in the list of Pet Animals" } else { Write-Output "Animal is not Pet Animal" } }
Output
PS C:\> PetAnimalsCheck -Animal Dog Animal is in the list of Pet Animals PS C:\> PetAnimalsCheck -Animal Tiger Animal is not Pet Animal
If we replace the above commands with the ValidateSet attribute, code will be of few lines.
Function PetAnimalsCheck { param( [ValidateSet("Cow","Dog","Cat","Horse","Camel","Elephant",ErrorMessage="Ani mal Name is not among list")] [String]$Animal ) Write-Output "Animal is Pet Animal" }
Output
PS C:\> PetAnimalsCheck -Animal Tiger PetAnimalsCheck: Cannot validate argument on parameter 'Animal'. Animal Name is not among list PS C:\> PetAnimalsCheck -Animal Cat Animal is Pet Animal
If you need the set to Case sensitive, use the IgnoreCase value.
Function PetAnimalsCheck { param( [ValidatepathExi] [ValidateSet("Cow","Dog","Cat","Horse","Camel","Elephant",ErrorMessage="Ani mal Name is not among list or case sensitive", IgnoreCase=$false)] [String]$Animal ) Write-Output "Animal is Pet Animal" }
Output
PS C:\> PetAnimalsCheck -Animal cat PetAnimalsCheck: Cannot validate argument on parameter 'Animal'. Animal Name is not among list or case sensitive PS C:\> PetAnimalsCheck -Animal Cat Animal is Pet Animal
- Related Articles
- How to use the ValidateRange attribute in PowerShell function?
- How to use the ValidateScript attribute in PowerShell function?
- How to use the ValidateCount attribute in PowerShell Function?
- How to use the ValidateLength attribute in PowerShell?
- How to use function Alias in PowerShell?
- Explain the Mandatory Attribute in PowerShell Advanced Function.
- How to use the formmethod attribute in HTML?
- How to use the required attribute in HTML?
- How to use stopwatch in PowerShell?
- How to use the ErrorActionPreference variable in PowerShell?
- How to use the ErrorAction parameter in PowerShell?
- How to use the tree command in PowerShell?
- How to use the Timeout command in PowerShell?
- How to add help in the PowerShell function?
- How to add an attribute to the XML file using Powershell?

Advertisements