PowerShell Scripts, Tips, Expert Advices, Forums, and Resources

image rotation

Welcome to PowerShell.com, the educational and community site for Windows PowerShell People. Get a quick overview.


As a Powershell.com member you will have access to:

  • Daily PowerShell tips written by Microsoft MVPs and other leading Windows PowerShell experts
  • Free Windows PowerShell advice and training provided by Microsoft MVPs and other leading Windows PowerShell experts
  • Access to leading Windows PowerShell blogs
  • A free ebook, Mastering PowerShell, written by Microsoft MVP Dr. Tobias Weltner
PowerTip of the Day

Testing Whether Text Contains Upper Case

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!

Copyright 2012 PowerShell.com. All rights reserved.