list users with specific username format

Welcome Forums General PowerShell Q&A list users with specific username format

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 6 years, 11 months ago.

  • Author
    Posts
  • #6270

    Member
    Points: 0
    Rank: Member

    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 "^[a-zA-Z]{3}\d{3}$"}

    Where

      ^ — 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

    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.

The topic ‘list users with specific username format’ is closed to new replies.