Get-ADUser -Filter

Rising Flight 5,216 Reputation points
2021-09-03T17:38:39+00:00

Hi All

i have email addresses in csv format, i want to export their samaccount names and UPNs to a csv file i am using the below syntax but i am not getting the output. please guide me

$list = Import-Csv C:\temp\list.csv
ForEach($user in $list){    
$dn = $user.user
Get-ADUser -Filter { displayName -like $dn } -Properties *| Select samaccountname,displayname,UserPrincipalName |Export-Csv C:\temp\export.csv -NoType -Append } 
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-09-03T18:58:42.387+00:00

    Hi @Rising Flight ,

    the file only contains one column with the email-address?

    If so, delete the first line (header) and try this please:

     ******@contoso.com  
     ******@contoso.com  
          
      $emailAddresses = Get-Content C:\temp\listcsv # Import-csv isn't required because it's only one column in the file  
      ForEach ($emailAddress in $emailAddresses) {      
          Get-ADUser -Filter { mail -like $emailAddress } -Properties * | Select-Object samaccountname, displayname, UserPrincipalName |   
          Export-Csv C:\temp\output.csv -NoType -Append   
      }  
    

    If there are more columns in the csv please try this:

     emailAddress  
     ******@contoso.com  
     ******@contoso.com  
          
      $emailAddresses = Import-Csv C:\temp\listcsv  
      ForEach ($emailAddress in $emailAddresses) {      
          Get-ADUser -Filter { mail -like $emailAddress.emailAddress } -Properties * | Select-Object samaccountname, displayname, UserPrincipalName |   
          Export-Csv C:\temp\output.csv -NoType -Append   
      }  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2021-09-03T19:02:57.32+00:00

    Using "ForEach (<expression){<code>}" does not emit an object into the success stream.

    Use this format of ForEach-Object instead:

    Import-Csv C:\temp\listcsv |
        ForEach-Object{    
            Get-ADUser -Filter { mail -eq $emailAddress } -Properties * | Select-Object samaccountname, displayname, UserPrincipalName
        } | Export-Csv C:\temp\output.csv -NoType -Append
    

    Since email addresses must be unique, if your CSV file contains the user's entire SMTP address use the "-eq" comparison operator instead of "-like".

    1 person found this answer helpful.
    0 comments No comments

  2. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-09-03T18:04:30.717+00:00

    Hi @Rising Flight ,

    if the csv file contains email addresses you need to filter on mail property. Or got I something wrong?

    $emailAddresses = "mmouse@test.local", "dduck@test.local"  
    ForEach ($emailAddress in $emailAddresses) {      
        Get-ADUser -Filter { mail -like $emailAddress } -Properties * | Select-Object samaccountname, displayname, UserPrincipalName |   
        Export-Csv C:\temp\exportUsers.csv -NoType -Append   
    }  
    

    The result looks like this:

    "samaccountname","displayname","UserPrincipalName"  
    "mmouse","Mickey Mouse","mmouse@testdomain.local"  
    "dduck","Donald Duck","dduck@testdomain.local"  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  3. Rising Flight 5,216 Reputation points
    2021-09-03T18:41:34.693+00:00

    i am getting below error when import from csv file

    Get-ADUser : Invalid type 'System.Management.Automation.PSCustomObject'. Parameter name: mail At line:3 char:6

    •  Get-ADUser -Filter { mail -like $emailAddress } -Properties * |  ...
      
    •  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      
      • CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException
      • FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
      emailAddress user1@Company portal .com user2@Company portal .com $emailAddresses = Import-Csv C:\temp\listcsv ForEach ($emailAddress in $emailAddresses) {
      Get-ADUser -Filter { mail -like $emailAddress } -Properties * | Select-Object samaccountname, displayname, UserPrincipalName | Export-Csv C:\temp\output.csv -NoType -Append }
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.