Testing Whether Text Contains Upper Case

by Sep 16, 2014

All PowerShell Versions

Use regular expressions to check whether a string contains at least one upper case letter:

$text1 = 'this is all lower-case'
$text2 = 'this is NOT all lower-case'

$text1 -cmatch '[A-Z]'
$text2 -cmatch '[A-Z]' 

The expected result is "True" and "False".

To check whether a text contains purely lower-case letters, try this:

$text1 = 'this is all lower-case'
$text2 = 'this is NOT all lower-case'

$text1 -cmatch '^[a-z\s-]*$'
$text2 -cmatch '^[A-Z\s-]*$' 

The expected result is "True" and "False".

Basically, this test is harder because you need to include all characters that you consider legal. In this example, I chose the lower-case letters from a to z, whitespace, and the minus sign.

These "legal" characters are embedded within "^" and "$" (line start and line end). The star is a quantifier (any number of "legal" characters).

Twitter This Tip! ReTweet this Tip!