- 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 ValidateCount attribute in PowerShell Function?
The validateCount attribute in PowerShell function is to validate the length of an array, which means that you can pass the specific number of arguments into the parameter. In the below example we need array should contain a minimum 1 and maximum 4 values when we pass the values. For that, we will write the below script,
Function ValidateArray { Param ( [ValidateCount(1,3)] [String[]]$Animals ) return $PSBoundParameters }
Output
PS C:\> ValidateArray -Animals Cow, Dog, Cat Key Value --- ----- Animals {Cow, Dog, Cat}
The above output is valid but when we pass the null or 4 values, it becomes invalid because we have declared an array should have a length between 1 to 3.
PS C:\> ValidateArray -Animals @() ValidateArray: Cannot validate argument on parameter 'Animals'. The parameter req uires at least 1 value(s) and no more than 3 value(s) - 0 value(s) were provided. PS C:\> ValidateArray -Animals Cow, Dog, Cat, Tiger ValidateArray: Cannot validate argument on parameter 'Animals'. The parameter req uires at least 1 value(s) and no more than 3 value(s) - 4 value(s) were provided.
- Related Articles
- How to use the ValidateRange attribute in PowerShell function?
- How to use the ValidateSet Attribute in PowerShell function?
- How to use the ValidateScript 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