title: Get sAMAccount from AD with powershell
body: <p>I have a CSV file that contains DisplayNames. I am able to read the CSV file, populate the sAMAccount and e-mail address, and save it to another CSV file with the code below.</p>
  <p>I now want to add two additional function to this:
  1- append the file and add this info to the rows next to each name,<br />
  2 - where a user is not found on AD, populate 'Not found' for sAMAccount and e-mail.</p>
  <pre><code>$usernames = Import-Csv  &quot;filepath.csv&quot; | ForEach-Object {
  Get-ADUser -Filter &quot;name -like '$($_.name)'&quot; -Properties name| 
  Select-Object SamAccountName, userPrincipalName} | Export-Csv -Path filepath.csv
  </code></pre>
  <p><strong>Example of csv</strong></p>
  <p><strong><em>name</em></strong><br />
  john block<br />
  Helen auntie<br />
  joe uncle</p>
  <p><strong>What export file looks like</strong></p>
  <p>&quot;SamAccountName&quot;,&quot;userPrincipalName&quot;<br />
  &quot;johnb01&quot;,&quot;john.block@mail.com&quot;<br />
  &quot;joeu01&quot;,&quot;joe.uncle@mail.com&quot;</p>
  <p><strong>what I want the file to look</strong></p>
  <p>&quot;name&quot;, &quot;SamAccountName&quot;, &quot;userPrincipalName&quot;<br />
  &quot;john block&quot;, &quot;johnb01&quot;, &quot;john.block@mail.com&quot;<br />
  &quot;Helen auntie&quot;, &quot;not found&quot;, &quot;not found&quot;&quot;<br />
  &quot;joe uncle&quot;, &quot;joeu01&quot;,&quot;joe.uncle@mail.com&quot;</p>