Powershell to Validate Email addresses

I'm trying to get Powershell to validate email addresses using Regex

Don't!

I would recommend against this. Accurately validating email addresses using regular expressions can be much more difficult than you might think.

Let's have a look at your regex pattern:

^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$

In it's current form it incorrectly validates [email protected].

On the other hand, it doesn't validate unicode-encoded internationalized domain names, like user@☎.com (yes, that's a valid email address)


Instead of trying to find or construct a perfect email validation regex pattern, I would use the MailAddress class for validation instead:

function IsValidEmail { 
    param([string]$EmailAddress)

    try {
        $null = [mailaddress]$EmailAddress
        return $true
    }
    catch {
        return $false
    }
}

If the input string is a valid email address, the cast to [mailaddress] will succeed and the function return $true - if not, the cast will result in an exception, and it returns $false.


When exporting the data, I'd consider collecting all the results at once in memory and then writing it to file once, at the end.

If you're using PowerShell version 2 or 3, you can do the same with two passes of Where-Object:

$EmailAddresses = Import-Csv "C:\Emails\OriginalEmails\emailAddresses.csv" -Header FirstName, LastName, Email, Address, City, State, ZipCode  | Select -Skip 1

$valid   = $list |Where-Object {IsValidEmail $_.Email}
$invalid = $list |Where-Object {-not(IsValidEmail $_.Email)}

If you're using PowerShell version 4.0 or newer, I'd suggest using the .Where() extension method in Split mode:

$EmailAddresses = Import-Csv "C:\Emails\OriginalEmails\emailAddresses.csv" -Header FirstName, LastName, Email, Address, City, State, ZipCode  | Select -Skip 1

$valid,$invalid = $list.Where({IsValidEmail $_.Email}, 'Split')

before exporting to file:

if($valid.Count -gt 0){ 
    $valid |Export-Csv "C:\Emails\ValidEmails\ValidEmails.csv" -NoTypeInformation
}
if($invalid.Count -gt 0){ 
    $invalid |Export-Csv "C:\Emails\ValidEmails\InvalidEmails.csv" -NoTypeInformation
}

You can just use the -match operator, instead of calling into the [Regex] class. Here's a simple example, without any wrapper function:

$EmailRegex = '^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$'

$EmailList = @('[email protected]', '[email protected]', '[email protected]')

foreach ($Email in $EmailList) {
  $DidItMatch = $Email -match $EmailRegex
  if ($DidItMatch) {
    # It matched! Do something.
  }
  else {
    # It didn't match
  }
}

FYI, when you use the -match operator, if it returns boolean $true, then PowerShell automatically populates a built-in (aka. "automatic") variable called $matches. To avoid unexpected behavior, you might want to reset this variable to $null during each iteration, or just wrap it in a function as you did in your original example. This will keep the variable scoped to the function level, as long as you don't declare it in one of the parent scopes.

Once you've validated the e-mail address, you can append it to your existing CSV file, using:

Export-Csv -Append -FilePath filepath.csv -InputObject $Email

For efficiency with the available filesystem resources, you'll probably want to buffer a few e-mail addresses in memory, before appending them to your target CSV file.

# Initialize a couple array buffers
$ValidEmails = @()
$InvalidEmails = @()

if ($ValidEmails.Count -gt 50) {
  # Run the CSV export here
}

if ($Invalid.Count -gt $50) {
  # Run the CSV export here
}

If you need further help, can you please edit your question and clarify what isn't working for you?


Each of the current top 2 answers here has one significant deficiency:

@Trevor's answer would do just fine, until you supply it this:

John Doe <[email protected]>

@Mathias' answer preaches about accommodating exceptional (yet valid) addresses such as those with non-ASCII or no TLD suffix. The following addresses all validate successfully with the [mailaddress] casting:

olly@somewhere | olly@somewhere. | [email protected]  etc

If, like me, you will not be entertaining these edge cases into your email databases, then a combination of both ideas might prove more useful, like so:

function IsValidEmail { 
    param([string]$Email)
    $Regex = '^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$'

   try {
        $obj = [mailaddress]$Email
        if($obj.Address -match $Regex){
            return $True
        }
        return $False
    }
    catch {
        return $False
    } 
}

Perhaps there is a performance overhead with creating $obj for every email address on a possibly long mailing list. But I guess that's another matter.