New in PowerShell 6 - Positive And Negative Parameter Validation

If you’ve written at least a couple of advanced PowerShell functions, you’re probably no stranger to parameter validation. These are the attributes you attach to parameters to make sure that they match a certain regular expression using [ValidatePattern()], or that when they are plugged into a certain script, that it evaluates to true using [ValidateScript({})]. You’ve probably also used [ValidateRange()] to make sure a number falls between a min and a max value that you specified.

In PowerShell 6, though, there’s something new and cool you can do with ValidateRange. You can specify in a convenient new syntax that the value must be positive or negative.

To do this, you start with a normal ValidateRange attribute, and instead of providing a range of numbers, you just use the word “Positive” or “Negative”, like this.

[ValidateRange('Positive')]$int = 10
[ValidateRange('Negative')]$int = -10

These will both work correctly because we’re assigning a value that works with the validation we’ve specified. Here’s two that will throw errors.

[ValidateRange('Positive')]$int = -10
[ValidateRange('Negative')]$int = 10

Here’s what it looks like in the console.

Neat, right?

Written on May 30, 2018