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-Log "Started running $($MyInvocation.MyCommand)" -Emphasis -Detailed
    }
    PROCESS
    {
        Write-Log "Checking for Validity on the following Email Addresseses" -Detailed
        if ($PassedEmailAddressArray -eq $Null)
        {
            Return $False
        }
        $IsValidEmailAddress=$True
        ForEach ($EmailAddress in $PassedEmailAddressArray)
        {
            Try
            {
                New-Object System.Net.Mail.MailAddress($EmailAddress)
            }
            Catch
            {
                $IsValidEmailAddress=$False
            }
        }
        Return $IsValidEmailAddress
    }
    END
    {
        Write-Log "Stopped running $($MyInvocation.MyCommand)" -Emphasis -Detailed
    }
}

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)
            }
            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)
            }
            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.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s