PowerShell: Check if an Email Address is Valid

There’s a pretty simple way to test if something is a valid email address but it’s nice to wrap that around with a bit of code to test for a stream or array of values.

The function and explanation follows.Here’s the full function with an explanation (and alternative way to make it work) afterwards.

<# .SYNOPSIS Returns if a string is a valid email address. Will also check all the elements of an array of email addresses. .DESCRIPTION Returns if a string is a valid email address. Will also check all the elements of an array of email addresses. .PARAMETER PassedEmailAddressArray This holds the email address to check (or array). .NOTES Name : Get-IsValidEmailAddress Author : HerringsFishBait.Com #>
function Get-IsValidEmailAddress
{
    param
    (
        [Parameter(ValueFromPipeline=$true)]
        [string[]]$PassedEmailAddressArray=@()
    )
    BEGIN
    {
        Write-Verbose "Started running $($MyInvocation.MyCommand)" 
    }
    PROCESS
    {
        Write-Verbose "Checking for Validity on the following Email Addresseses"
        if ($PassedEmailAddressArray -eq $Null)
        {
            Return $False
        }
        $IsValidEmailAddress=$True
        ForEach ($EmailAddress in $PassedEmailAddressArray)
        {
            Try
            {
                New-Object System.Net.Mail.MailAddress($EmailAddress) > $null
            }
            Catch
            {
                $IsValidEmailAddress=$False
            }
        }
        Return $IsValidEmailAddress
    }
    END
    {
        Write-Verbose "Stopped running $($MyInvocation.MyCommand)"
    }
}

The function is built around trying to create a new System.Net.Mail.MailAddress object from the email address string you’ve passed.  If it triggers an error then the string isn’t a valid email address.

    param
    (
        [Parameter(ValueFromPipeline=$true)]
        [string[]]$PassedEmailAddressArray=@()
    )

This part allows us to pass an array of email addresses, stream a set of email addresses or even pass a stream of email address arrays.

The current logic is;

        $IsValidEmailAddress=$True
        ForEach ($EmailAddress in $PassedEmailAddressArray)
        {
            Try
            {
                New-Object System.Net.Mail.MailAddress($EmailAddress) > $null
            }
            Catch
            {
                $IsValidEmailAddress=$False
            }
        }
        Return $IsValidEmailAddress

If this processes an array then the function returns $False if ANY of them are not valid.  You could also do;

        ForEach ($EmailAddress in $PassedEmailAddressArray)
        {
            Try
            {
                New-Object System.Net.Mail.MailAddress($EmailAddress) > $null
            }
            Catch
            {
                Write-Output=$False
            }
        }

Which would write $False to the output for each element that wasn’t correct in the array.  You could then use more complicated logic to pull out the correct email addresses.

2 Replies to “PowerShell: Check if an Email Address is Valid”

    1. Thanks! It relied on a log-processing function (Write-Log) I’ve written about earlier; I replaced the calls with Write-Verbose and it was all good!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: