list users with specific username format

by taotao07 at 2013-02-06 10:53:30

So, here’s an interesting one for you… Currently we’re using abc1234 format for our usernames, so three letters and four digits. I just noticed that someone has been creating new users with abc123 format, so three letters and three digits. What would be a query to list only users with abc123 UID format? I’ve tried several where-object {$.samaccountname -like …} combinations but I can’t seem to come up with the right one. Any help is greatly appreciated.
Thanks!
by ArtB0514 at 2013-02-06 11:54:52
Instead of -Like, use -Match and a regex.
Where-Object {$
.samAccountName -match "[1]{3}\d{3}$"}

Where[list]
^ – start of string
[a-zA-Z] – include all lower and upper case letters
{3} – repeat exactly 3 times
\d – any digit
{3} – repeat exactly 3 times
$ – end of string[/list]

For an introduction to regular expressions, see http://msdn.microsoft.com/en-us/library/ms972966.aspx.
by taotao07 at 2013-02-06 12:05:08
Works like a charm, thank you!
by ArtB0514 at 2013-02-06 13:46:07
You’re very welcome.


  1. a-zA-Z ↩︎