Easy PowerShell Email Address Validation Function

Easy PowerShell Email Address Validation Function

5 / 5 ( 1 vote )

While updating the my Public Folder IDFix tool, I was testing the [Net.Mail.MailAddress] class.  I’ve used it in the past, and it seemed to be a pretty simply way to check for valid addresses, but I started running into problems with it not detecting some things that were obviously not in the RFC.

Here’s how I fixed* it.


I originally liked using [Net.Mail.MailAddress] class for a variety of reasons:

  • I liked that it was standard as part of the operating system, so it should be available across installations.
  • I hate writing regular expressions.

Basically, if you’ve ever spent any time as a sysadmin, this can be boiled down to I’m lazy. Someone else already did this work, so why should I re-invent the wheel?  Well, it seems to work differently than it did in the past (or maybe I just didn’t test all of the use cases).  Here’s the net-net of an example of using it:

New-Object Net.Mail.MailAddress("AddressToValidate")


New-Object Net.Mail.MailAddress("aaron.guilmette@microsoft.com")

If you don’t test too hard, it looks like it’s successful:

However, Net.Mail.MailAddress also “validates” things that aren’t RFC-compliant for internet email routing, such as addresses that don’t have a valid suffix (at least 1 character separated by a period followed by two more characters) or not ending the ‘user’ side of the email address with a period (“.”) character.

Azure AD and Exchange Online use a much more strict syntax. It’s possible for addresses (like aaron.@microsoft.com) to pass Net.Mail.MailAddress, but fail validation in the cloud. I’m trying to avoid things that result in troubleshooting during the middle of a migration.


So, to help work around that, I have simple(ish) regular expression that addresses a lot of major cases:


To wrap it up in a simple function, you can do something like this:

function ValidateEmail($address)
     $address -match "^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"

If it returns True, the address is likely validly formatted.  If it returns false, it’s bad.

So, you can use it in a script to validate a bunch of stuff like this:

$list = @('a@b.com','a.@b.com','a+b@c.dc','a>@c')
foreach ($i in $list) 
  if (ValidateEmail $i) 
    "$($i) is valid"
    "$($i) is invalid."


*Posts about email regexes invariably draw a lot of comments, so I want to offer some pre-buttals:

  • Yes, I’m aware that it’s not a perfect email regular expression. There are plenty that are more complex to capture larger character classes and more edge cases.
  • Yes, I know that it isn’t verifying if it’s deliverable–only that it’s formatted reasonably well.
  • Yes, I know that I’m not taking all the non-English validations into consideration.



Published by Aaron Guilmette

Helping companies conquer inferior technology since 1997. I spend my time developing and implementing technology solutions so people can spend less time with technology. Specialties: Active Directory and Exchange consulting and deployment, Virtualization, Disaster Recovery, Office 365, datacenter migration/consolidation, cheese. View all posts by Aaron Guilmette

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Exit mobile version