Checking Text Ending with Wildcards

by Apr 5, 2012

In a previous tip you learned how to use the string method EndsWith() to check whether a text ends with certain characters. This method does not accept wildcards, though.

So to check text endings for patterns, try this approach instead:

PS> "Account123" -match '\d{3}$' 
True

This uses a regular expression to check for text that ends with three ("{3}") digits ("\d") at the end of a line ("$").

And this would be a use case: listing all files in a folder that end with three digits:

PS> Dir $env:windir\system32 | Where-Object { $_.BaseName -match '\d{3}$' }

Twitter This Tip! ReTweet this Tip!