Wednesday, 7 December 2011

Confirm-ValidEmailAddress.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script validates email addresses based on 
  4.     MSFT published Regular Expression. This is a  
  5.     re-write with PowerShell of an existing bit of  
  6.     MSDN sample code  
  7. .DESCRIPTION 
  8.     This script first creates a function to validate 
  9.     an email address. It uses a large regex that is 
  10.     documented at the MSDN page noted below. The script 
  11.     then creates an array of email addreses and then 
  12.     validates them against the function and displays 
  13.     the results. 
  14. .NOTES 
  15.     File Name  : Confirm-ValidEmailAddress.ps1 
  16.     Author     : Thomas Lee - tfl@psp.co.uk 
  17.     Requires   : PowerShell Version 2.0 
  18. .LINK 
  19.     This script posted to: 
  20.         http://pshscripts.blogspot.com/2011/12/confirm-validemailaddressps1.html 
  21.     MSDN sample posted to: 
  22.         http://msdn.microsoft.com/en-us/library/01escwtf.aspx  
  23. .EXAMPLE 
  24.     Valid: david.jones@proseware.com 
  25.     Valid: d.j@server1.proseware.com 
  26.     Valid: jones@ms1.proseware.com 
  27.     Invalid: j.@server1.proseware.com 
  28.     Invalid: j@proseware.com9 
  29.     Valid: js#internal@proseware.com 
  30.     Valid: j_9@[129.126.118.1] 
  31.     Invalid: j..s@proseware.com 
  32.     Invalid: js*@proseware.com 
  33.     Invalid: js@proseware..com 
  34.     Invalid: js@proseware.com9 
  35.     Valid: j.s@server1.proseware.com 
  36.     Valid: tfl@psp.co.uk 
  37.     Valid: cuddly.penguin@cookham.net 
  38. #> 
  39.  
  40. Function IsValidEmail   { 
  41. Param ([string] $In
  42. # Returns true if In is in valid e-mail format. 
  43. [system.Text.RegularExpressions.Regex]::IsMatch($In,  
  44.               "^(?("")(""[^""]+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +  
  45.               "(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$");  
  46. } # End of IsValidEmail 
  47.     
  48. [string[]] $emailAddresses = "david.jones@proseware.com", "d.j@server1.proseware.com",  
  49.                             "jones@ms1.proseware.com", "j.@server1.proseware.com",  
  50.                             "j@proseware.com9", "js#internal@proseware.com",  
  51.                             "j_9@[129.126.118.1]", "j..s@proseware.com",  
  52.                             "js*@proseware.com", "js@proseware..com",  
  53.                             "js@proseware.com9", "j.s@server1.proseware.com"
  54.                             "tfl@psp.co.uk", "cuddly.penguin@cookham.net"  
  55.                              
  56. ForEach ($emailAddress in $emailAddresses)    { 
  57.   if (IsValidEmail($emailAddress)) { 
  58.        "Valid: {0}" -f $emailAddress 
  59.        } 
  60.   else
  61.         "Invalid: {0}" -f $emailAddress 
  62.        }     
  63. }                                         

1 comment:

bytehead said...

Maybe this is just plain silly, but j..s@... and j.@... ARE valid email accounts. Which is a problem when trying to use regular expressions on email addresses. You might be able to get away with validating the domain part, but pretty much anything can go for the account name.