Use regex to validate PowerShell parameters

Among the more arcane languages in common use is regular expressions (or regex). Regex is both insanely powerful and remarkably obscure (at least to me).

I needed an all alphabetic, lowercase variable in a PowerShell script I am working on. Sure, I could use the ToLower() method of a [string] variable. But I’m stubborn and wanted to ensure a parameter entered on the command line conforms to the requirements. This means using regex since built-in PowerShell parameter checking is somewhat limited to things like ValidateNotNull and ValidateCount, among others.

I guess the PowerShell developers thought, “The developer can do whatever he or she wants in regex, so if we add a way to use regex via ValidatePattern, we’re covered.”

And that’s true, except regex is so darn complicated. In any event, here’s my (minuscule) contribution to the searchable canon of regex in PowerShell: a simple script that accepts a parameter consisting only of the lowercase letters a through z. It uses this parameter to display processes with that name on the console. I use Google Chrome, so I tested with.\powershellregex.ps1 -lowercasealpha "chrome"which will show Chrome processes (tabs). 

If you happen to supply a non-valid character, as I did below by suppling “Chrome”, you get a PowerShell sea-of-red error with the regex but no explanation of the actual violation. I kinda appreciate the assumption in this: if you use regex, you should know what it means.

PowerShell ValidatePattern error when regex not met
PowerShell ValidatePattern error when regex not met (click to enlarge)
<#
    .SYNOPSIS
        Demonstrate simple regex in input parameters
    
    .DESCRIPTION
        Shows how to use regex to require an all lowercase, alphabetic entry for an input parameter.
    
    .PARAMETER lowercasealpha
        Name of process to display
    
    .EXAMPLE
        PS C:\> powershellregex.ps1 -lowercasealpha 'lowercaselettersfromathroughzonly'
    
    .NOTES
        Alex Neihaus 2017-09-05
        (c) 2017 Air11 Technology LLC -- licensed under the Apache OpenSource 2.0 license, https://opensource.org/licenses/Apache-2.0
        Licensed under the Apache License, Version 2.0 (the "License");
        you may not use this file except in compliance with the License.
        You may obtain a copy of the License at
        http://www.apache.org/licenses/LICENSE-2.0
 
        Unless required by applicable law or agreed to in writing, software
        distributed under the License is distributed on an "AS IS" BASIS,
        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        See the License for the specific language governing permissions and
        limitations under the License.
 
        Author's blog: https://www.yobyot.com
#>
[CmdletBinding()]
param
(
    [Parameter(Mandatory = $true,
               ValueFromPipeline = $true,
               HelpMessage = 'Lowercase, alphabetic name of process to display')]
    [ValidatePattern('(?-i:^[a-z]+$)')]
    [string]$lowercasealpha
)

# Show processes with names contain $lowercasealpha
Get-Process | Where-Object -Property ProcessName -Contains -Value $lowercasealpha

Posted

in

,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *