My friends,
With Active Dirctory,
I have a two-liner here that almost works.
The Regex works fine,
$regexfilter = '^[A-Za-z]+\.[A-Za-z]+[A-Za-z0-9]?[0-9]$'
Get-ADUser -Filter "samaccountname -like '*'" | where { $_.samaccountname -match $regexfilter} | select Enabled, mail, Name, sn, Samaccountname,givenname,cn,MIDDLENAME, INITIALS,uSDAAgencyAbbreviation,uSDAOfficeID,DisplayName
The output at the console is wrong. The mail and other attributes are blank, but I know there are good values in the Active Directory Record.
Here is the output:
Enabled : True
mail :
Name : John.Jordan3
sn :
Samaccountname : John.jordan3
givenname : John
cn :
MIDDLENAME :
INITIALS :
DefenseAgencyAbbreviation :
DefenseOfficeID :
DisplayName :
The purpose is to show all users in Active Directory that match the Regex.
But Regex cannot be used with -Filter.
So my logic is:
Look at all the users: -Filter “samaccountname -like ‘*’”
Then match the Regex: -match $regexfilter
I suspect I need to Splat, but I don’t know how to do that here.
Thank you in advance for your help.
And if you know of a better way to get this done, I would appreciate learning your approach.
The Regex returns True for things like:
Joe.Jones01
Joe.Jones4