title: Escape apostrophes from variable in powershell
body: <p>I have the following script set up, and it works as intended....except for users with apostrophes in their email.</p>
  
  <pre><code>$users = get-content C:\Filename.csv
  
  $results = Foreach ($user in $users)
  {
  
  get-aduser -filter "EmailAddress -like '$user'" -properties CanonicalName,LastLogon,Name | 
  select Name,@{N="Thing";E={$_.DistinguishedName.Split(',')[-4]}},@{N="Place";E={$_.DistinguishedName.Split(',')[-5]}},@{N="Last Logon";E `
  ={[DateTime]::FromFileTime($_.Lastlogon)}}
  
  }
  
  
  $results | Export-Csv C:Filename.csv -NoTypeInformation
  </code></pre>
  
  <p>Does anyone know how to escape the apostrophe in an email like <code>example_o'connor@example.com</code>? Whenever I run the above script it always errors out on email addresses like the one above.</p>
  
  <p>Edit: My csv file is a single column of email addresses with no header.</p>
  
  <p>Edit 2: Sorry to waste everyone's time. Turns out the script was working fine, along with some of the suggestions in this thread such as adding "" "" around my variable, but it was that I was testing against a list where not all the values were correct so that is my fault. Thank you everyone for your time.</p>