in powershell how can i filter list of mail address with a regexp ?

Hello,

i need to get all mails that are not formed like following :
 givenname.lastname@domain.ext

so far i only have the list of email out of the AD as follow :

get-aduser -Filter * -Properties UserPrincipalName, emailaddress | select UserPrincipalName, EmailAddress | Export-Csv "M:\tmp\mail_not_conform.otl"

how can i add such regexp in my ps ?

the other email adress dont have a "." to separate two informations.

thank you in advance for your help.

Toshi.
Erwin PombettAsked:
Who is Participating?
 
Dan CraciunIT ConsultantCommented:
? is short for where, which is short for where-object.

Can you please post some email addresses that should of not been filtered?

[a-zA-Z-]+\.[a-zA-Z-]+@[a-z0-9-]+\.\w+
should take care of names and domains with -
0
 
Dan CraciunIT ConsultantCommented:
get-aduser -Filter * -Properties UserPrincipalName, emailaddress | select UserPrincipalName, EmailAddress | ? EmailAddress -notmatch "\w+\.\w+@\w+\.\w+"| Export-Csv "M:\tmp\mail_not_conform.otl"

Open in new window

HTH,
Dan
0
 
Erwin PombettAuthor Commented:
hello Dan,

thank you for the reply.

as complement :

1. what do "?" means after pipe ?
2. givennames or names with a  "-"  also still appear, how to add them the filter ?

Thank you for further help.

toshi
0
 
footechCommented:
You could approach this in a couple ways.  Do you want a regex pattern such that the email address just has a dot separating a couple portions in front of the @ symbol, or do you want to make sure that the email address actually matches the account's firstname and lastname and has a specific domain after the @ symbol?  Dan's will give you the first.  Here's an example of the second.
get-aduser -Filter * -Properties UserPrincipalName, emailaddress | % { $fn = $_.givenname; $ln = $_.surname; $_ | ? EmailAddress -ne "$fn.$ln@domain.ext" } | select UserPrincipalName, EmailAddress | Export-Csv "M:\tmp\mail_not_conform.otl"

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.