Export AD UPN and SamAccount Name

Roger Roger 4,951 Reputation points
2021-04-14T01:57:37.7+00:00

Hi All

i have email addresses in csv file, my csv file is in below format. How can i export samaccountname, UPN by importing the email addresses.

user
user1@Company portal .com
user2@Company portal .com

When i try the below syntax i am getting error

foreach($user in (get-content C:\temp\input.csv){
get-aduser -filter {emailaddress -eq $user} |select -property displayname, samaccountname,UserPrincipalName, emailaddress |
Export-Csv C:\temp\output.csv -NoTypeInformation
}

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,843 questions
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,174 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,345 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,571 Reputation points Microsoft Vendor
    2021-04-14T05:16:01.03+00:00

    Hi,

    Please check to see if this works.

    $input = "C:\temp\input.csv"  
    $output = "C:\temp\output.csv "  
    Import-Csv -Path $input | ForEach-Object {  
        $mail = $_.user  
        Get-ADUser -Filter {EmailAddress -eq $mail} -Properties DisplayName, EmailAddress |   
            Select-Object -Property DisplayName, SamAccountName,UserPrincipalName, EmailAddress  
    } | Export-Csv -Path $output -NoTypeInformation  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

4 additional answers

Sort by: Most helpful
  1. Muthu Kumar 1 Reputation point
    2022-12-19T14:29:56.587+00:00

    Hi,

    It is not working getting an error as mail is not valid one

    Regards
    Muthu


  2. Muthu Kumar 1 Reputation point
    2022-12-19T16:16:49.947+00:00

    i am looking for where if i given sam account name need the upn id
    input the samaccount ame and ran the above script getting the error as below
    Get-ADUser : Variable: 'mail' found in expression: $mail is not defined.

    not sure where ii am wrong
    thanks
    Muthu

    0 comments No comments

  3. Rich Matheisen 44,776 Reputation points
    2022-12-19T16:49:03.24+00:00

    Assuming your CSV contains a column named "samaccountname" you won't need the "-Filter" because the CSV's samaccountname can be used as the value for the Get-ADUser "-Identity" parameter:

    $in = "C:\temp\input.csv"  
    $out = "C:\temp\output.csv "  
    Import-Csv -Path $in |   
        ForEach-Object {  
            Get-ADUser -Identity $_.samaccountname -Properties DisplayName, EmailAddress |   
                Select-Object -Property DisplayName, SamAccountName, UserPrincipalName, EmailAddress  
        } | Export-Csv -Path $out -NoTypeInformation  
    
    0 comments No comments

  4. Muthu Kumar 1 Reputation point
    2022-12-19T17:54:00.797+00:00

    Thanks a lot you saved my day .

    0 comments No comments