---
title: "Mastering PowerShell ValidateScript for Better Input"
description: "Learn the best ways to use PowerShell ValidateScript validation attribute for efficient parameter input and validation."
canonical: "https://adamtheautomator.com/powershell-validatescript/"
---

# Mastering PowerShell ValidateScript for Better Input

> Learn the best ways to use PowerShell ValidateScript validation attribute for efficient parameter input and validation.

Source: https://adamtheautomator.com/powershell-validatescript/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Mastering PowerShell ValidateScript for Better Input](https://adamtheautomator.com/wp-content/uploads/2019/10/sieve-2202240_1920.jpg)

# Mastering PowerShell ValidateScript for Better Input

[![](https://secure.gravatar.com/avatar/9a14f10ff1b1ec7d790d34f5b559e4d3de2d31b172e6ef266dfd8b479174d97b?s=192&d=mm&r=g)June Castillote](https://adamtheautomator.com/author/june/)16 October 201910 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)

Table of Contents

*   [Parameter Validation (An Analogy)](#parameter-validation-an-analogy-)
*   [Parameter Validation Logic Flow](#parameter-validation-logic-flow)
*   [Walkthrough Requirements](#walkthrough-requirements)
*   [Understanding the ValidateScript Validation Attribute](#understanding-the-validatescript-validation-attribute)
*   [Using ValidateScript in a Function](#using-validatescript-in-a-function)
*   [Build the Function](#build-the-function)
*   [Adding Parameter Validation](#adding-parameter-validation)
*   [Displaying More Meaningful Error Messages](#displaying-more-meaningful-error-messages)
*   [Adding Custom Validation Error Using Windows PowerShell (5.1)](#adding-custom-validation-error-using-windows-powershell-5-1-)
*   [Adding Custom Validation Errors Using Using PowerShell Core (6+)](#adding-custom-validation-errors-using-using-powershell-core-6-)
*   [PowerShell ValidateScript Usage Examples](#validatescript-usage-examples)
*   [Date Parameter Validation](#date-parameter-validation)
*   [Windows Process Parameter Validation](#windows-process-parameter-validation)
*   [Validating Parameters by Another Parameter Value](#validating-parameters-by-another-parameter-value)
*   [Using ValidateScript in the Console](#using-validatescript-in-the-console)
*   [Validating Integer Value](#validating-integer-value)
*   [Validating a Date Value](#validating-a-date-value)
*   [Summary](#summary)
*   [Further Reading](#further-reading)

When creating PowerShell [functions](https://adamtheautomator.com/powershell-functions/), validating input to parameters is important. Parameter validation allows you to limit what is passed to functions. In this article, you’re going to learn how to catch problems before they become one with the PowerShell _ValidateScript_ validation attribute.

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

Using parameter validation, you not only ensure problems don’t arise once the function runs but also promotes cleaner code since the validation logic is not placed within the body of the function.

There are several [advanced parameter](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters) validation attributes available in Powershell. But in this article, you will learn about one of the most flexible parameter validations, PowerShell _ValidateScript_ validation.

You’ll see how it works and also see examples of how to use it.

## **Parameter Validation (An Analogy)**

![Parameter Validation](/wp-content/uploads/2019/10/Untitled.png)

Parameter Validation

I was having lunch and I suddenly felt like having a soda. I walked to the soda machine, pulled out a ₱100 bill from my pocket and inserted it to the bill slot. The machine spits it out immediately.

I then noticed that just right above the slot it says it only accepts ₱20 and ₱50 bills. I ended up not having the soda because I only had that ₱100 bill with me.

And how does the story relate to [PowerShell parameter](https://adamtheautomator.com/powershell-parameter/) validation? Let’s break it down.

*   The soda machine serves as the function
*   The bill is the parameter
*   The ₱50 and ₱20 bills are the valid parameter values
*   The ₱100 was rejected because it is the wrong parameter value
*   As a result, the machine did not process my request because of the wrong input. Hence, no soda for me. ….and I was thirsty too!

The soda machine scenario above is just one analogy to describe the concept of parameter validation.

## Parameter Validation Logic Flow

The concept of parameter validation follows a rough workflow. The image below shows an overview of how parameter validation works.

![PowerShell Parameter Validation Flow](https://adamtheautomator.com/wp-content/uploads/2020/06/parameter-validation-works..png)

PowerShell Parameter Validation Flow

1.  The function is executed in PowerShell by calling its name and providing the parameter values
2.  PowerShell evaluates the provided values.
3.  If the result of the validation is true, PowerShell will allow the function to continue its process, before exiting.
4.  If the result of the validation is false, PowerShell will display an error and the function will terminate.

## Walkthrough Requirements

More than just talking about PowerShell function parameter validation, the rest of this article will have examples that you can copy and try on your own. To follow along, you’ll need a few things to follow along.

*   A Windows 10 computer with any of the PowerShell versions below:
*   Windows PowerShell 5.1
*   PowerShell Core 6.2
*   PowerShell Core 7 (latest release of this writing is Preview 4)
*   A script editor of your choice. Like, [Notepad++](https://notepad-plus-plus.org/downloads/), [PowerShell ISE](https://docs.microsoft.com/en-us/powershell/scripting/components/ise/introducing-the-windows-powershell-ise), or [Visual Studio Code](https://code.visualstudio.com/).

## Understanding the **ValidateScript Validation Attribute**

_ValidateScript_ is one of the parameter validation attributes available for use in PowerShell introduced in PowerShell 3.0. It can be added inside the parameter definition block of a function, or it can also be used directly in the PowerShell console.

_ValidateScript_ is used to validate the value of the parameter you entered. If the validation result is `$true`, then the script will continue to run. On the other hand, if the result is `$false`, the function will throw a terminating error.

Let’s dive in and see how it works.

## Using ValidateScript in a Function

The most common use of the _ValidateScript_ attribute is attached to a function parameter. In this section, you’ll create a dummy function and apply _ValidateScript_ parameter validation.

The dummy function will perform the following actions:

1.  Accept input for the amount.
2.  Validate if the amount entered is equal to accepted values.
3.  Run the process if the validation passed.
4.  Display an error if the validation failed.

You will learn how to build this function step by step in the next section. Fire up your script editor and begin coding!

### Build the Function

First, create a function and give it a name. This can be any name you choose. It is best practice to follow the Verb-Noun naming convention to make your function descriptive. For this example, use the name `Get-Soda`.

```powershell
Function Get-Soda {
    [CmdletBinding()]
    param ()
}
```

Inside the `param()` block, insert the name of the parameter you will be using which is `Bill`.

```powershell
Function Get-Soda {
    [CmdletBinding()]
    param ( 
        $Bill
    )
}
```

At this point, the function does not perform anything yet but to accept any value for the `Bill` parameter.

Ensure you’ve copied the `Get-Soda` function into your PowerShell console. Once the function is imported into the PowerShell session, test the function by running the command: `Get-Soda -Bill 100`.

When you run the `Get-Soda` function, you will notice no error is thrown and it does nothing. That’s expected at this point.

### Adding Parameter Validation

Perhaps you do not want to allow all values to be passed to the `Bill` parameter. Using the analogy explained at the top of this article, the function shouldn’t allow ₱100 bills. It should only allow ₱50 and ₱20 bills.

Add the _ValidateScript_ parameter validation by inserting `[ValidateScript()]`before the `Bill` parameter. Your function should look like the code below.

```powershell
Function Get-Soda {
    [CmdletBinding()]
    param (
        [ValidateScript()]
        $Bill 
    )
}
```

Inside of the `[ValidateScript()]`block, insert the validation code `{$_ -eq 20}`. This validation code checks if the value provided to the bill parameter is equal to 20. The below snippet is how the code should look like.

> _Note: The `$_` represents the value of the current parameter in scope. In this example, the value of `$_` is the value of the parameter `Bill`._

```powershell
Function Get-Soda {
    [CmdletBinding()]
    param (
        [ValidateScript({$_ -eq 20})]
        $Bill
    )
}
```

Now run this function again to confirm that the parameter validation is working by running:

```powershell
PS51> Get-Soda -Bill 20
PS51> Get-Soda -Bill 30
```

Below you can see that when 20 is passed as a value, nothing happens but when anything other than 20 is passed, it throws an error.

![Error](https://adamtheautomator.com/content/images/2019/10/Untitled--2-.png)

Error

When the `Get-Soda` function doesn’t throw an error, that means the function executed successfully. To demonstrate that, add the following code inside of the function to simply return a message to the console.

```powershell
Write-Host "$Bill is an accepted value. Please select your soda."
```

The `Get-Soda` function will then look like below:

```powershell
Function Get-Soda {
    [CmdletBinding()]
    param (
        [ValidateScript({$_ -eq 20})]
        $Bill 
    )

    Write-Host "$Bill is an accepted value. Please select your soda."
}
```

Now pass an accepted value like 20 to the `Bill` parameter. You should see the result below.

![Output of Get-Soda -bill 20](https://adamtheautomator.com/wp-content/uploads/2020/06/20-to-the-Bill-paramete.png)

Output of Get-Soda -bill 20

## Displaying More Meaningful Error Messages

I’m sure you’ve noticed by now that the error message returned when the parameter value does not pass validation is not very intuitive, and ugly.

![Validation error is unclear](https://adamtheautomator.com/wp-content/uploads/2020/06/very-intuitive-and-ugly..png)

Validation error is unclear

Sadly, there is nothing you can do about how the error looks. Until a feature that enables the formatting of the validation errors is released (hopefully), you’re stuck with it.

But, it is possible to improve it a little bit and give more meaning to the errors which can benefit your users. You will see the example of how to do that next.

### Adding Custom Validation Error Using Windows PowerShell (5.1)

Expanding upon the `Get-Soda` function, it’s possible to throw specific error message when the value does not meet validation. To do this, you can add an if/then construct inside of the _ValidateScript_ block.

Create a simple if/then construct like in the below example. In this example, if the `Bill` parameter value is not equal to 20, it will return an error message of your choosing (`X is invalid. Valid value is 20 only.`).

You can see an example of what this looks like below:

```powershell
Function Get-Soda {
    [CmdletBinding()]
    param (
        [ValidateScript({
        if ($_ -eq 20) {
            $true
        } else {
            throw "$_ is invalid. Valid value is 20 only."
        }
    })]
    $bill
    )
    process {
        Write-Host "$bill is an accepted value. Please select your soda."
    }
}
```

The screenshot below shows the custom error message in action. The error still looks ugly, but this time the message is clear and understandable.

![ValidateScript with a custom error message (PowerShell 5.1)](https://adamtheautomator.com/wp-content/uploads/2020/06/Adding-Custom-V.png)

ValidateScript with a custom error message (PowerShell 5.1)

### Adding Custom Validation Errors Using Using PowerShell Core (6+)

Starting with PowerShell Core 6, the ability to add custom error messages to _ValidateScript_ parameter validation is already built-in. Let’s call this the _ErrorMessage technique_.

In the modified `Get-Soda` function code below, the `[ValidateScript()]` block now includes an `ErrorMessage` property which can be used in place `throw`. Now instead of using the `$_` variable, you can use `{0}` which will represent the parameter value passed.

```powershell
Function Get-Soda {
    [CmdletBinding()]
    param (
        [ValidateScript({
            $_ -eq 20            
        },
        ErrorMessage = "{0} is invalid. Valid value is 20 only."
        )]
        $Bill
	)
    Write-Host "$Bill is an accepted value. Please select your soda."
}
```

The screenshot below shows the expected output is exactly the same as with using the _if-else and throw technique_.

![ValidateScript with a custom error message (PowerShell Core 7 preview 4)](https://adamtheautomator.com/wp-content/uploads/2020/06/using-the-if-else-and-throw-technique..png)

ValidateScript with a custom error message (PowerShell Core 7 preview 4)

## PowerShell ValidateScript Usage Examples

These are some real-world use case scenarios for which _ValidateScript_ can be applied. Review and test these examples, then try to improve the error message on your own using the techniques you learned in the previous section.

### Date Parameter Validation

This snippet is a function that accepts a start and an end date. It performs validation that the dates entered are not in the future and are not older than 90 days.

```powershell
<#
    Dummy function to search logs.
    Accepts the following parameters.

        * startDate
            - date must not be older than 90 days.
            - date must not be in the future.
        * endDate
            - date must not be older than 90 days.
            - date must not be in the future.
#>
Function Search-Log {
    [CmdletBinding()]
    param (
        [parameter(Mandatory)]
        [ValidateScript({
            ($_ -gt (Get-Date -Hour 0 -Minute 0 -Second 0).AddDays(-90) -and $_ -le (Get-Date))
        })]
        [datetime]$startDate,


        [parameter(Mandatory)]
        [ValidateScript({
            ($_ -gt (Get-Date -Hour 0 -Minute 0 -Second 0).AddDays(-90) -and $_ -le (Get-Date))
        })]
        [datetime]$endDate
    )

    process {
        Write-Host "I will search the logs between [$startDate] and [$endDate"]
    } 
}
```

Test the function by using these commands. You can specify your own `startDate` and `endDate` values

```powershell
#TEST: Dates are valid
$startDate = (Get-Date).AddDays(-90) #Today - 90 days
$endDate = (Get-Date) # Today
search-log -startDate $startDate -endDate $endDate
```

This sample output below is when valid date values are entered.

![Search-Log result with valid date parameter values](https://adamtheautomator.com/wp-content/uploads/2020/06/1-38.png)

Search-Log result with valid date parameter values

Now test it again with `startDate` that is older than 90 days. Then confirm that the error message is displayed.

### Windows Process Parameter Validation

This next snippet is a function that accepts the name of a process. It confirms that the process is running in memory, then kills all running instances of that process or exit depending on the validation result.

```powershell
<#
    A function to kill all instances of a process.
    Usage: Kill-Process -name <process name>
    Example: Kill-Process -name notepad
#>

Function Kill-Process {
    [CmdletBinding()]
    param (
        [parameter(Mandatory)]
        [ValidateScript(
            {
                if (Get-Process -Name $_) {
                    $true
                }
                else {
                    throw "A process with name $_ is not found."
                }
            }
        )]
        [string]$Name
    )
    process {
        Write-Host "Killing Process: $Name"
        Stop-Process -Name $Name -Force
    }
}
```

Test it using this command: `Kill-Process -Name <process name>`

The sample output below assumes that you are testing with the process named _notepad._

*   The first command ran successfully because it found the _notepad_ process was running and proceeded to kill it.
*   The second command failed since _notepad_ is no longer running and the function ended with the custom error message you created.

![Parameter validation error](https://adamtheautomator.com/wp-content/uploads/2020/06/error-message-you-created..png)

Parameter validation error

## Validating Parameters by Another Parameter Value

Now you should have an idea of the value of using _ValidateScript_ in your functions. Bu there is one limitation that is worth mentioning. _ValidateScript_‘s scope is only within the parameter in use. This means that one parameter cannot use the value of other parameters.

As a workaround, cross-parameter value checking can be done by accessing the [`$PSBoundParameters`](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-6#psboundparameters) automatic variable.

Perhaps you have a function called `Send-Spam`. This function has three parameters:

1.  `From` – The sender’s email address. (eg. [a@a.com](mailto:a@a.com)). Not mandatory.
2.  `To` – The recipient’s email address. (eg. [b@b.com](mailto:b@b.com)). Not mandatory.
3.  `SendEmail` – A switch parameter that does not require a value. But if used, the `From` and `To` parameter values will be required.

If the `SendEmail` switch is used, a validation code will run to confirm that the `From` and `To` parameters are used. Then it will return the value of `True`. On the other hand, if `From` and `To` are not in use, the function will throw an error and exit.

Copy and paste the code below into your PowerShell session.

```powershell
<#
    Dummy function to send spam
    Accepts the parameters
        
        From
            - sender email address
            - cannot be null or empty
        To
            - recipient email address
            - cannot be null or empty
        SendEmail
            - a switch (on/off) parameter.
            - it will validate if 'To' and 'From' values are present.

    Usage: Send-Spam -From a@a.com -To $b@b.com -SendEmail
#>

Function Send-Spam {
    [CmdletBinding()]
    param (
        [parameter()]
        [ValidateNotNullOrEmpty()] #Ensure From is not equal to $null or ""
        [mailaddress]$From,

        [parameter()]
        [ValidateNotNullOrEmpty()] #Ensure To is not equal to $null or ""
        [mailaddress]$To,

        [parameter()]
        [ValidateScript(
            {
                # Check if the From and To parameters are specified

                if ($PSBoundParameters.Keys -contains 'From' -AND
                    $PSBoundParameters.Keys -contains 'To') {
                        $true
                }
                else {
                    throw "From and To parameters are required when using SendEmail"
                }
            }
        )]
        [switch]$SendEmail
    )

    process {
        Write-Host "From: $From" -ForegroundColor Yellow
        Write-Host "To: $To" -ForegroundColor Cyan
        if ($SendEmail) {
            Write-Host "Sending Spam from '$From' to '$To'" -ForegroundColor Green
        }        
    }
}
```

The command shown below will result in a successful validation. Because the `From` and `To` parameters were used along with the `SendEmail` switch.

```powershell
PS51> Send-Spam -From a@a.com -To b@b.com -SendEmail
```

![Output from Send-Spam](https://adamtheautomator.com/wp-content/uploads/2020/06/Now-test-it-without-using-t.png)

Output from Send-Spam

Now test it without using the `From` or the `To` parameter. This should result in an error because the validation script will fail. See the example result below.

![Running Send-Spam without the To parameter](https://adamtheautomator.com/wp-content/uploads/2020/06/Now-test-it-without-using-t1.png)

Running Send-Spam without the To parameter

## Using ValidateScript in the Console

Although the most common use of the _ValidateScript_ attribute is to used to validate function parameters, it can also be used directly from the PowerShell console. _ValidateScript_ is useful for testing your validation code even before incorporating it inside a script or a function.

Below are some examples of using _ValidateScript_ outside of a function.

### Validating Integer Value

This example below validates that a given integer value is greater than five.

```powershell
[ValidateScript({$_ -gt 5})]$i=4
```

After running the above code, the expected result fails because the given value is 4 which is less than 5. The example screenshot below is what you’d expect to see.

![Validation failed](https://adamtheautomator.com/wp-content/uploads/2020/06/ample-screenshot-below-is-what-youd-expect-to-see..png)

Validation failed

### Validating a Date Value

The next example code below shows how to validate that the given date is newer than or equal to the current date.

```powershell
[DateTime][ValidateScript({$_ -ge (Get-Date)})]$date = (Get-Date)
[DateTime][ValidateScript({$_ -ge (Get-Date)})]$date = (Get-Date).AddHours(-1)
```

After running the first line of the code in the above snippet, the expected result is passed (True) since the provided _DateTime_ value is the same or a few seconds greater.

The second line of code will produce a failed result because the _DateTime_ value is one hour less than the current time. See the screenshot below for the sample output.

![Date parameter validation failure](https://adamtheautomator.com/wp-content/uploads/2020/06/See-the-screenshot-below-for-the-sample-output..png)

Date parameter validation failure

## Summary

In this article, you learned what parameter validation is and how to use the _ValidateScript_ parameter validation attribute.

You also learned how to expand its capability to display a more descriptive error message. While following the examples, you have experienced the results of both successful and failed validation.

You should now understand the limitation of _ValidateScript_ in terms of cross-parameter referencing, and how you can workaround that limitation by utilizing the `$PSBoundParameters` automatic variable.

I hope you learned enough in this article that you can apply in your PowerShell tool-making journey!

## Further Reading

*   [**_Powershell Functions Introduction_**](https://adamtheautomator.com/powershell-functions/)
*   [**_Understanding and Building PowerShell Modules_**](https://adamtheautomator.com/powershell-modules/)
*   [**_ValidateScript validation attribute_**](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-6#validatescript-validation-attribute)
*   [**_About Throw_**](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_throw?view=powershell-6)

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-validatescript%2F&text=Mastering%20PowerShell%20ValidateScript%20for%20Better%20Input)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-validatescript%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-validatescript%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
