What are the Validating Parameters with PowerShell ValidateScript [Walkthrough]?

Validating parameters in PowerShell are responsible for limiting what has been passed to a function. While creating a function, it is important to validate the input parameters. Basically, it is a set of instructions that limits the users to enter particular values to the specific domain. It can validate arrays, integers, boolean, or strings.

This post will outline the validation parameters of the PowerShell ValidateScript.

What are the Validating Parameters With PowerShell ValidateScript [Walkthrough]?

As we have learned that validation is the process of limiting something to a specific number. For instance, it limits the user to entering five wrong passwords. After that, it will lock the user to enter an entry.

Let’s explore some of the given examples.

Example 1: Validate an Array Parameter

Run the below code in order to validate an array parameter in PowerShell:

function Test-Array {
        [ValidateScript({"$_.Count -gt 1"})]
    Write-Output "Array contains $($Values.Count) values."
Test-Array -Values "one", "two"
Test-Array -Values "apple", "mango", "cherry"


In the above-stated code:

  • First, define a function named “Test-Array”.
  • Then, specify a validating parameter that the count should be greater than “1”.
  • After that, add the parameter that needs to be evaluated by passing the values with the help of the validating parameter.
  • Lastly, invoke the defined function by passing the values in accordance with the specified condition in the validating parameter:

Example 2: Validate an Integer Parameter

Executing the below code will validate an integer parameter:

function Test-Integer {
        [ValidateScript({"$_ -gt 0"})]
        if($Number -gt 0){
        Write-Output "Provided number is positive."}
        Write-Output "Provided number is negative."}        
Test-Integer -Number -1


Following the above code:

  • Define a function “Test-Integer”.
  • In its definition, the validating parameter refers to the condition where the past parameter should have a count greater than zero.
  • Then, specify another parameter that needs to be evaluated.
  • Now, place the condition in the “if-else” statement, such that if the condition is in accordance with the validating parameter the “if” statement comes into effect.
  • Otherwise, the else statement will be executed.
  • Finally, invoke the defined function having a number less than zero. Thereby resulting in an un-satisfied validating parameter condition:

That’s it!  We have briefly explained about validating parameters with PowerShell ValidateScript.


Validation parameters or validating parameters are the set of rules that restrict the users to enter specific values to the specific domain. It operates to provide the validation of the input parameters. This blog has overviewed the validating parameters in PowerShell.

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.