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 (            
    [ValidateSet('laptop','desktop')]            
    [ValidatePattern('(?-i:^[a-z]+$)')]            
    [string]$Type            
)            
    $Type            
}

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)]            
    [string]$Type            
)            
    $Type            
}

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…