none
get-aduser from a csv file contain email address to provide DistinguishedName RRS feed

  • Question

  • I was given a spreadsheet with email address. Was asked to provide DistinguishedName

    spreadsheet only contain email address on the first column

    sample:

    john@abc.com

    steave@abc.com 

    but using the powershell script is not working

    Get-Content -Path "C:\Script\GetDN\Users.txt" | % {Get-ADUser -Identity $_} | Select-Object -Property SamAccountName, DistinguishedName | Export-Csv C:\Script\GetDN\DN.csv

    • Edited by Kt, Monday, June 25, 2018 7:47 AM na
    Monday, June 25, 2018 7:46 AM

Answers

  • Sorry.  It has to be like this:

    Get-Content C:\Script\GetDN\Users.txt | 
        ForEach-Object{Get-ADUser -Filter "Mail -eq '$_'"} |
        Select-Object SamAccountName, DistinguishedName 


    \_(ツ)_/

    • Marked as answer by Kt, Monday, June 25, 2018 8:33 AM
    Monday, June 25, 2018 8:14 AM

All replies

  • An email address is not an identifier.  You do not load a CSV with Get-Content.

    If you have a text file with a list of emails then this will work assuming the emails are correct.

    Get-Content C:\Script\GetDN\Users.txt | 
        ForEach-Object{Get-ADUser -Filter "Mail -eq '$_'"} |
        Select-Object SamAccountName, DistinguishedName 


    \_(ツ)_/


    • Edited by jrv Monday, June 25, 2018 8:15 AM
    Monday, June 25, 2018 7:54 AM
  • Hi Jrv,

    i change .csv to .txt and contain 1 mail address (testing propose), but below errors pops out.

    PS C:\Windows\system32> Get-Content D:\Kent\GetDN\Users.txt | 
        ForEach-Object{Get-ADUser -Filter "Mail -eq $_"} |
        Select-Object SamAccountName, DistinguishedName 
    Get-ADUser : Error parsing query: 'Mail -eq kent.lee@abc.com' Error Message: 'syntax error' at position: '10'.
    At line:2 char:20
    +     ForEach-Object{Get-ADUser -Filter "Mail -eq $_"} |
    +                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ParserError: (:) [Get-ADUser], ADFilterParsingException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Manageme 
       nt.Commands.GetADUser

    Monday, June 25, 2018 8:05 AM
  • Sorry.  It has to be like this:

    Get-Content C:\Script\GetDN\Users.txt | 
        ForEach-Object{Get-ADUser -Filter "Mail -eq '$_'"} |
        Select-Object SamAccountName, DistinguishedName 


    \_(ツ)_/

    • Marked as answer by Kt, Monday, June 25, 2018 8:33 AM
    Monday, June 25, 2018 8:14 AM
  • Hi Jrv

    is working now, you save my day.

    Monday, June 25, 2018 8:33 AM