I forgot to update one piece of the below code. I'm no longer returning all 
properties. I have it only returning the additional property of 'OfficePhone':

$StdUsers = Get-ADuser -Filter {Enabled -eq $True} -Properties OfficePhone | 
Where {$_.samAccountName -match "^[A-Z]{2}[0-9]{3,6}"}


From: Orlebeck, Geoffrey
Sent: Monday, August 29, 2016 10:28 AM
To: 'powershell@lists.myitforum.com'
Subject: Get-ADUser and RegEx:

All:

I'm attempting filter user accounts based on samAccountName formatting. The 
standard we use is user's initials + employee ID. For example: AB1234. The 
issue I ran into is there are some test/service accounts using a similar format 
(e.g. TEST1234) being returned when I used the initial regular expression of 
"[A-Z]{2}[0-9]{3,}". I added a carrot to the beginning and it now is excluding 
accounts that don't begin with two letters. I did a spot check across my 
results and while it appears now only accounts matching our standard user 
criteria are returned, I was hoping for a sanity check:

This returns standard (e.g. AB1234) and non-standard accounts (e.g. TEST1234):
$StdUsers = Get-ADuser -Filter {Enabled -eq $True} -Properties * | Where 
{$_.samAccountName -match "[A-Z]{2}[0-9]{3,6}"}

This only returns standard account naming conventions (e.g. AB1234, CD56789):
$StdUsers = Get-ADuser -Filter {Enabled -eq $True} -Properties * | Where 
{$_.samAccountName -match "^[A-Z]{2}[0-9]{3,6}"}


I was hoping this group could help me confirm two things:

1)    The "^[A-Z]{2}[0-9]{3,6}" regular expression is the best one for matching 
accounts like "AB1234" while ignoring "TEST1234". One question I have is would 
"^[A-Z]{2}[0-9]{3,6}" still match on something like "AB1234TEST"? I am not sure 
if what I have is explicitly looking from the start of the samAccountName for 
two letters followed by 3-6 digits, and ignoring any characters beyond.

2)    Is my filtering and matching formatted efficiently using standard 
cmdlets? I tried using a regular expression within the -Filter parameter, but 
the error I received makes me think I cannot filter with regular expressions 
(at least the Get-ADUser cmdlet doesn't support it). I know the mantra "filter 
left, format right", still looking if there is a better way to accomplish this 
goal.

In case it's relevant, I'm doing some AD attribute settings for phone number 
formatting-some users are "(555) 555-5555" or "5555555555" and we're moving 
towards a standard "555-555-5555", but I first only want to pipe standard user 
accounts.

Thank you for your time.

-Geoff
Confidentiality Notice: This is a transmission from Community Hospital of the 
Monterey Peninsula. This message and any attached documents may be confidential 
and contain information protected by state and federal medical privacy 
statutes. They are intended only for the use of the addressee. If you are not 
the intended recipient, any disclosure, copying, or distribution of this 
information is strictly prohibited. If you received this transmission in error, 
please accept our apologies and notify the sender. Thank you.



Reply via email to