---
title: "Mastering PowerShell Parameter Validation with Dynamic Methods"
description: "Boost Your Scripting Skills! Dive into Dynamic PowerShell Parameter Validation for Efficient and Robust Coding. A Must-Read for PowerShell Excellence."
canonical: "https://adamtheautomator.com/powershell-parameter-validation/"
---

# Mastering PowerShell Parameter Validation with Dynamic Methods

> Boost Your Scripting Skills! Dive into Dynamic PowerShell Parameter Validation for Efficient and Robust Coding. A Must-Read for PowerShell Excellence.

Source: https://adamtheautomator.com/powershell-parameter-validation/

---

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 Parameter Validation with Dynamic Methods](https://adamtheautomator.com/wp-content/uploads/2019/06/5d238ae5c824b514689ea58a.jpg)

# Mastering PowerShell Parameter Validation with Dynamic Methods

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)1 February 20244 min. read

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

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

Table of Contents

*   [Creating a Dynamic Powershell Parameter Validation the Hard Way](#creating-a-dynamic-validation-parameter-the-hard-way)
*   [Creating a Dynamic ValidateSet Array Parameter the Easy Way](#creating-a-dynamic-validation-parameter-the-easy-way)

One of the coolest yet complex features of advanced functions in PowerShell is dynamic parameters and using that feature to perform PowerShell parameter validation. Dynamic parameters take your typical function parameters to a whole new level.

Have you ever had a time when you created an advanced function and wanted your parameters to depend on something else; to __dynamically__ be created based on the criteria you choose at runtime?

How about wanting [Powershell](https://adamtheautomator.com/tag/powershell/) parameter validation like building a PowerShell `ValidateSet` array providing tab-completion on a parameter not based on a static set of stings but generated at runtime? These are both doable with dynamic parameters.

* * *

> _This post is part of the PowerShell Blogging Week (#PSBlogWeek) series on Windows PowerShell Advanced Functions, a series of coordinated posts designed to provide a comprehensive view of a particular topic._

In this series, we also have:

*   [Standard and Advanced PowerShell Functions](https://lazywinadmin.com/2015/03/standard-and-advanced-powershell.html) by [Francois-Xavier Cat (@LazyWinAdm)](https://twitter.com/LazyWinAdm) (March 30, 2015)
*   [PowerShell Advanced Functions: Can we build them better? With parameter validation, yes we can!](https://mikefrobbins.com/2015/03/31/powershell-advanced-functions-can-we-build-them-better-with-parameter-validation-yes-we-can/)by[Mike F. Robbins (@mikefrobbins)](https://twitter.com/mikefrobbins) (March 31, 2015)
*   [Supporting WhatIf and Confirm in Advanced Functions](https://jdhitsolutions.com/blog/powershell/4319/powershell-blogging-week-supporting-whatif-and-confirm/) by [Jeffery Hicks (@JeffHicks)](https://twitter.com/JeffHicks) (April 2, 2015)
*   [Advanced Help for Advanced Functions](https://www.sapien.com/blog/2015/04/03/advanced-help-for-advanced-functions/) by [June Blender (@juneb\_get\_help)](https://twitter.com/juneb_get_help) (April 3, 2015)
*   [A Look at Try/Catch in PowerShell](https://learn-powershell.net/2015/04/04/a-look-at-trycatch-in-powershell/) by [Boe Prox (@proxb)](https://twitter.com/proxb) (April 4, 2015)

_To suggest a PowerShell Blogging Week topic, leave a comment or tweet it to us with the #PSBlogWeek hashtag._

* * *

There are a couple of different ways to use dynamic parameters that I’ve seen. The first is the way that [Ben Ten wrote about them on PowerShell Magazine](https://powershellmagazine.com/2014/05/29/dynamic-parameters-in-powershell/). Using this method, Ben was able to create parameters on the fly based on if a different parameter was used. Personally, I’ve never had a need to do this.

I really like using dynamic parameters as a way to __validate__ input based on some criteria that are available at runtime. This way I can write a script that gathers information on-the-fly which allows me the beautiful parameter tab-completion we all know and love.

Let’s go over an example on how to create [Powershell parameter](https://adamtheautomator.com/powershell-parameter/ "Powershell parameter") validation based on files in a folder.

“Normal” advanced function parameters allow you to use a few [`Validate` options](https://msdn.microsoft.com/en-us/library/ms714432%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396). You can validate the number of arguments a parameter can accept, the minimum and maximum length of a parameter argument, a set of options in an array, matching a regex string or a scriptblock and more. What I’m looking for here is to use the [`ValidateSet`](https://adamtheautomator.com/powershell-validateset/ "ValidateSet") attribute for the tab-completion.

![Tab completion in PowerShell](https://adamtheautomator.com/content/images/2019/07/dynamic-parameters-and-parameter-validation---TabCompletion.gif)

Tab completion in PowerShell

You’ll notice in the example above I’m using the `Get-Item` cmdlet and the default parameters for tab-completion which is to be expected. I want that functionality but I want to tab-complete my own arguments so let’s create a simple function to do that.

![The PowerShell ValidateSet parameter validation attribute](https://adamtheautomator.com/content/images/2019/07/dynamic-parameters-and-parameter-validation---validate_set_example.png)

The PowerShell ValidateSet parameter validation attribute

You’ll notice that I’ve highlighted the validation attribute that will allow us to tab-complete the `MyParameter` argument. Now we’re able to get custom parameter argument tab-completion using the values specified in the PowerShell `ValidateSet` array attribute.

![Tab-completion with dynamic parameters](https://adamtheautomator.com/content/images/2019/07/dynamic-parameters-and-parameter-validation---validate_Set_example-1.gif)

Tab-completion with dynamic parameters

But now what if I want my tab-completion options to be generated on-the-fly based on some other criteria rather than a static list? The only option is to use dynamic parameters. In my example, I want to tab-complete a list of files in a particular folder at run-time.

To get this done I’ll be using a dynamic parameter which will run [`Get-ChildItem`](https://adamtheautomator.com/get-childitem/ "Get-ChildItem") whenever I try to tab-complete the `MyParameter` parameter.

With that being said, let’s make the `ValidateSet` attribute of the `MyParameter` parameter dynamic, shall we?

The first difference between a standard parameter and a dynamic parameter that you’ll notice is dynamic parameter are in their own block.

```powershell
[CmdletBinding()]
param()
DynamicParam {

}
```

## Creating a Dynamic Powershell Parameter Validation the Hard Way

Inside the `DynamicParam` block is where the magic happens. And the magic does take a while to wrap your head around.

A dynamic parameter is, in a sense, a `System.Management.Automation.RuntimeDefinedParameterDictionary` object with one or more `System.Management.Automation.RuntimeDefinedParameter` objects inside of it. But it’s not quite that easy. Let’s break it down.

1.  First, instantiate a new `System.Management.Automation.RuntimeDefinedParameterDictionary` object to use as a container for the one or more parameters we’ll be adding to it using

```powershell
$RuntimeParamDic = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary.
```

2\. Next, create the `System.Collections.ObjectModel.Collection` prepped to contain `System.Attribute` objects.

```powershell
$AttribColl = New-Object System.Collections.ObjectModel.Collection[System.Attribute].
```

3\. Now instantiate a `System.Management.Automation.ParameterAttribute` object which will hold all of the parameter attributes we’re used to. In our instance, I’m defining my parameter to be in all the parameter sets and accept pipeline input by a pipeline object and by property name.

```powershell
$ParamAttrib = New-Object System.Management.Automation.ParameterAttribute
$ParamAttrib.Mandatory = $Mandatory.IsPresent
$ParamAttrib.ParameterSetName = '__AllParameterSets'
$ParamAttrib.ValueFromPipeline = $ValueFromPipeline.IsPresent
$ParamAttrib.ValueFromPipelineByPropertyName = $ValueFromPipelineByPropertyName.IsPresent
```

4\. Add our parameter attribute set to the collection we instantiated above.

```powershell
$AttribColl.Add($ParamAttrib)
```

5\. Because I’m using this dynamic parameter to build a PowerShell ValidateSet array for parameter validation I must also include a `System.Management.Automation.ValidateSetAttribute` object inside of our attribute collection. This is where you define the code to actually create the values that allows us to tab-complete the parameter arguments.

```powershell
$AttribColl.Add((New-Object System.Management.Automation.ValidateSetAttribute((Get-ChildItem C:\TheAwesome -File | Select-Object -ExpandProperty Name))))
```

6\. We then have to instantiate a `System.Management.Automation.RuntimeDefinedParameter` object using the parameter name, it’s type and the attribute collection we’ve been adding stuff to.

```powershell
$RuntimeParam = New-Object System.Management.Automation.RuntimeDefinedParameter('MyParameter', [string], $AttribColl)
```

7\. Once the run time parameter is finished we then come back to that original dictionary object we instantiated earlier using the parameter name and the runtime parameter object we created.

```powershell
$RuntimeParamDic.Add('MyParameter', $RuntimeParam)
```

****8.**** We can then return this runtime dictionary object back to the dynamic parameter block and we’re done!

```powershell
return $RuntimeDic
```

Are your eyes glazing over yet? Mine was when I first tried to figure this out.

Being the lazy admin I am I created a function called [`New-ValidationDynamicParam`](https://raw.githubusercontent.com/adbertram/Random-PowerShell-Work/master/PowerShell%20Internals/New-DynamicParam.ps1) that does all this work for you creating the PowerShell ValidateSet array. Simply pass in the parameter name, the attributes you’d like the parameter to have and the code you’ll be using to create the validation and you’re done! The function does the rest.

## Creating a Dynamic ValidateSet Array Parameter the Easy Way

```powershell
New-ValidationDynamicParam -Name 'MyParameter' -Mandatory -ValidateSetOptions (Get-ChildItem C:\TheAwesome -File | Select-Object -ExpandProperty Name)
```

My pain is your gain, people!

Now, with our dynamic validation parameter created, let’s take it for test drive.

I’ve got some files in a directory on my computer that I only want to be passed to the `MyParameter`parameter.

![Sample text files](https://adamtheautomator.com/content/images/2019/07/dynamic-parameters-and-parameter-validation---filelist.png)

Sample text files

Now all I have to do is run our script and voila! I’m now only able to use the file names as parameter arguments and they are updated as the files comes in and out of the folder!

![Using the dynamic parameter creation script](https://adamtheautomator.com/content/images/2019/07/dynamic-parameters-and-parameter-validation---dynamicparameterexample.gif)

Using the dynamic parameter creation script

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-parameter-validation%2F&text=Mastering%20PowerShell%20Parameter%20Validation%20with%20Dynamic%20Methods)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-parameter-validation%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-parameter-validation%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/)
