Case sensitive ValidateSet.

OK, I know it may be useless in 99% of cases, but lets say you fall in the 1%. You not only care about the string itself, but also about case used. E.g. you don’t want user to pollute produced objects with scary thing like: lApToP. What can you do?

So far I was sure only combination of ValidateSet and ValidatePattern could make it possible. And due to default regex behaviour in PowerShell – it would be complicated even there (need to explicitly disable IgnoreCase option also in regex pattern):

function Get-Foo {            
param (            

That would work, obviously. But recently I was reading docs for other Validate* class on MSDN, and out of curiosity I selected ValidateSet. And what I found? There is named option in it, IgnoreCase, that defaults to true. At first I could not figure out how to use it and set to false. Than I compared it with other attribute declarations and syntax for named parameters for those within PowerShell, like CmdletBinding (plenty of named parameters there). And because I use those in PowerShell a lot it was easy to translate the one in ValidateSet:

function Get-Bar {            
param (            
    [ValidateSet('laptop','desktop',IgnoreCase = $false)]            

It was not documented in PowerShell itself (or: I was not able to spot it in the help anywhere), so I thought it might be worth sharing. Puszczam oczko Just in case someone need it. I know I did and eventually added enum class to support this…


4 thoughts on “Case sensitive ValidateSet.

  1. [ValidatePattern('^[a-z]+$',Options='None')] $string
    There is a poorly documented option, with no examples, on the Microsoft page about this. Option=’None’ or Option=0, will set Case Sensitive. All the numbers can be added to create an enum.

    • Awesome, thanks! 🙂 I haven’t considered this, and it is for sure more obvious than disabling case-insensitivity directly in pattern.
      PS: I modified your comment a little bit, hope you don’t mind.. 🙂

      • FYI it should be “Options=0” (note the plural on option) not “Option=0”.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s