title: Powershell to get user attributes given samaaccount
body: <p>I have a notepad list of 100 users. Normally I use the below script to get all the users within one OU but this time I have users from different OU and I have to search using Samaccountname.</p>
  
  <pre><code>clear 
   $UserInfoFile = New-Item -type file -force "C:\Scripts\UserInfo.txt"  
  "Login`tGivenname`tEmail" | Out-File $UserInfoFile -encoding ASCII 
   Import-CSV "C:\Scripts\OU.txt" | ForEach-Object { 
    $dn = $_.dn 
    $ObjFilter = "(&amp;(objectCategory=User)(objectCategory=Person))" 
    $objSearch = New-Object System.DirectoryServices.DirectorySearcher 
    $objSearch.PageSize = 15000 
    $objSearch.Filter = $ObjFilter 
    $objSearch.SearchRoot = "LDAP://$dn" 
    $AllObj = $objSearch.FindAll() 
  foreach ($Obj in $AllObj) 
        { $objItemS = $Obj.Properties 
               $Ssamaccountname = $objItemS.samaccountname 
               $SsamaccountnameGN = $objItemS.givenname 
               $SsamaccountnameSN = $objItemS.sn 
               $SsamaccountnameEN = $objItemS.mail
               "$Ssamaccountname`t$SsamaccountnameGN`t$SsamaccountnameSN`t$SsamaccountnameEN" | Out-File $UserInfoFile -encoding ASCII -append 
        } # End of foreach
     } # End of ForEach-Object 
  </code></pre>
  
  <p>I am trying to use the list of samaccountname to get the name and email of those users. I am new to Powershell so the above script itself was a bit difficult for me to grasp and now I am on an even more difficult task. </p>