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


i need to get all mails that are not formed like following :

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.

Erwin PombettAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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.

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

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?

should take care of names and domains with -

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.