title: Powershell to Validate Email addresses
body: <p>I'm trying to get Powershell to validate email addresses using Regex and put email addresses into good and bad csv files. I can get it to skip one line and write to file, but cannot get it to target the email addresses and validate them, then write lines to good and bad files. I can do it in C# and JavaScript, but have never done it in Powershell. I know this can be done, but not sure how. </p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>Function IsValidEmail { 
      Param ([string] $In) 
      # Returns true if In is in valid e-mail format. 
      [system.Text.RegularExpressions.Regex]::IsMatch($In,  
          "^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|
      (([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");  
  } 
  
  
  ## Now we need to check the original file for invalid and valid emails.**
  
  $list = Get-Content C:\Emails\OriginalEmails\emailAddresses.csv
  
  # This way we also use the foreach loop.
  ##======= Test to see if the file exists ===========
  
  if (!(Test-Path "C:\Emails\ValidEmails\ValidEmails.csv")) {
      New-Item -path C:\Emails\ValidEmails -name ValidEmails.csv -type 
      "file" # -value "my new text"
      Write-Host "Created new file and text content added"
  }
  else {
      ## Add-Content -path C:\Share\sample.txt -value "new text content"
      Write-Host "File already exists and new text content added"
  }
  
  
  if (!(Test-Path "C:\Emails\InValidEmails\InValidEmails.csv")) {
      New-Item -path C:\Emails\InValidEmails -name InValidEmails.csv -type 
      "file" # -value "my new text"
      Write-Host "Created new file and text content added"
  }
  else {
      # Add-Content -path C:\Emails\ValidEmails -value "new text content"
      Write-Host "File already exists and new text content added"
  }
  
  #$Addresses = Import-Csv "C:\Data\Addresses.csv" -Header 
  Name, Address, PhoneNumber | Select -Skip 1
  $EmailAddressImp = Import-Csv 
  "C:\Emails\OriginalEmails\emailAddresses.csv" -Header 
  FirstName, LastName, Email, Address, City, State, ZipCode  | Select  
  FirstName, LastName, Email, Address, City, State, ZipCode -Skip 1
  </code></pre>
  
  <h1>I'm validating the third column "Email" in the original csv file and trying to write out the whole row to file (good file, bad file). Not sure how to buffer either doing this.</h1>
  
  <pre><code>ForEach ($emailAddress in $list) { 
      if (IsValidEmail($emailAddress)) { 
          "Valid: {0}" -f $emailAddress
          Out-File -Append C:\Emails\ValidEmails\ValidEmails.csv -Encoding UTF8
          $EmailAddressImp | Export-Csv "C:\Emails\ValidEmails\ValidEmails.csv" 
          -NoTypeInformation
      } 
      else { 
          "Invalid: {0}" -f $emailAddress 
          Out-File -Append C:\Emails\InValidEmails\InValidEmails.csv -
          Encoding UTF8
          $EmailAddressImp | Export-Csv 
          "C:\Emails\InValidEmails\InValidEmails.csv" -NoTypeInformation
      }     
  }                                         
  </code></pre>