locked
Get samaccount from email address RRS feed

  • Question

  • I have a list of email addresses and I'm trying to get the name or samaccount name using the script below but my out put is blank. 

    Any help would be greatly appreciated.

    Thanks,

    Andy

    Get-Content E:\users2.txt | ForEach {
    
        Get-ADUser -Filter "userprincipalname -eq '$_'" | Select SamAccountName | Export-Csv e:\newusers.csv
    
    }
    

    Monday, July 2, 2018 5:06 PM

Answers

  • It works on the userPrincipalName, but the text file must contain the full email address line by line without any quotes around the data.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Monday, July 2, 2018 5:49 PM

All replies

  • What does the content of user2.txt look like? Your code should work, so I am guessing it is something with that file.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Monday, July 2, 2018 5:27 PM
  • Or possibly you need to filter on the mail attribute instead of userPrincipalName.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Monday, July 2, 2018 5:35 PM
  • It works on the userPrincipalName, but the text file must contain the full email address line by line without any quotes around the data.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Monday, July 2, 2018 5:49 PM
  • Or possibly you need to filter on the mail attribute instead of userPrincipalName.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)


    Depending on the environment those users live in (Exchange/O365) the e-mail address may also be a secondary address and reside in the multi-valued emailaddresses property.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Monday, July 2, 2018 6:16 PM
  • The problem is that you are exporting every single item, so you just get overwritten the last one

    Get-Content E:\users2.txt | ForEach {

    Get-ADUser -Filter "userprincipalname -eq '$_'" | Select SamAccountName

    }| Export-Csv e:\newusers.csv

    Please use it like this.

    $example=@()
    
    Get-Content E:\users2.txt | ForEach {
        $example+= Get-ADUser -Filter "userprincipalname -eq '$_'" | Select SamAccountName
    }
    $example | Export-Csv e:\newusers.csv -notypeinformation
    
    

    $Example | Export-csv -notypeinformation E:\file.csv


    • Edited by j0rt3g4 Monday, July 2, 2018 7:00 PM
    Monday, July 2, 2018 6:56 PM
  • Your closing curly bracket is in the wrong place. The code below will work.

    Get-Content E:\users2.txt | %{Get-ADUser -Filter "userprincipalname -eq '$_'" } | 
    Select SamAccountName | Export-Csv e:\newusers.csv -append -NoType
    You can always use the append switch if you need to add the values to the existing .csv output file. If you want to overwrite it leave the append parameter out.

    Monday, July 2, 2018 11:01 PM
  • I would like to note that the rules of good programming style require you not to put everything on one or two lines.  It makes code difficult to manage, read and debug.

    This would be best:

    Get-Content E:\users2.txt | 
        ForEach-Object{
            Get-ADUser -Filter "userprincipalname -eq '$_'" 
        } | 
        Select SamAccountName | 
        Export-Csv e:\newusers.csv -append -NoType


    \_(ツ)_/

    Monday, July 2, 2018 11:54 PM
  • It works on the userPrincipalName, but the text file must contain the full email address line by line without any quotes around the data.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Clayman you're a genius!  There was a space after all of the UPNs.....   I removed the spaces and voila the script worked!

    Thank you very much!

    Andy

    Tuesday, July 3, 2018 10:20 AM