locked
need a powershell command to imput the email address then output the username of the user? then print this list. RRS feed

  • Question

  • My apologies but I have limited experience with writing scripts in powershell.

    need a powershell command to imput the email address then output the username of the user? then print this list.

    Also need to print out of this list.

    Thank You.


    dsk


    • Edited by kimdav111 Tuesday, September 6, 2016 3:03 PM correction
    Tuesday, September 6, 2016 2:47 PM

Answers

  • I want to imput the email address which is unique

    dsk

    You need to get the email addresses in a csv/txt file; e.g file.txt with the email column header called email

    //Import the email address

    $emails = (Import-Csv .\file.txt ).email

    Get details for each user.

    foreach($mail in $emails) {Get-ADUser -Properties mail -filter {mail -eq $mail} | | select name,mail, samaccountname >> Output.csv}

    $emails = (Import-Csv .\file.txt ).email
    
    foreach($mail in $emails) {Get-ADUser -Properties mail -filter {mail -eq $mail} | select name,mail, samaccountname >> Output.csv}

    ***PLEASE NOTE Output will need to be formatted in say excel to remove blank spaces and repeated headers


    • Proposed as answer by jrv Saturday, September 10, 2016 3:59 AM
    • Marked as answer by Richard MuellerMVP Friday, September 16, 2016 4:00 PM
    Saturday, September 10, 2016 3:50 AM

All replies

  • Hi,

    You can use Get-ADUser and Export-Csv cmdlets like below:

    Get-ADUser -Filter * -SearchBase "ou=Ou,dc=contoso,dc=com" -Properties * | Export-Csv  "c:\somefile.csv"

    Tuesday, September 6, 2016 2:56 PM
  • The question is, are first names (givenName) and last names (sn) in your domain unique?

    If they are not (and the probability of this is likely proportional to the number of user accounts in your domain), then how will you determine which user names correspond with which accounts?


    -- Bill Stewart [Bill_Stewart]

    Tuesday, September 6, 2016 3:03 PM
  • Finding the users depends on how they are setup in AD. Are the First Name and Last Name fields populated in ADUC?. Is the user Common Name in the form Last Name, First Name, or something else? Is the Display Name populated, and what is its format? Also, none of these are sure to uniquely identify users.  But sAMAccountName (the pre-Windows 2000 logon name) does uniquely identify the object in AD.

    Edit: We replied before you changed the question. You probably want to filter on the mail attribute (the corresponding PowerShell property exposed by the Get-ADUser cmdlet is EmailAddress). But if you have Exchange you may need to filter on proxyAddresses. This assumes the fields are populated and uniquely identify the users.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)



    Tuesday, September 6, 2016 3:07 PM
  • Since you've edited your question to say "email address" rather than "first and last name," this is easier.


    PS C:\> Get-ADUser -LDAPFilter "(mail=name@domain.local)"

    Of course replace name@domain.local with the email address you want to search for. This will output details for the user. The SamAccountName property in the output is the username.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, September 6, 2016 3:53 PM
  • how do I imput a list of email into this script?

    dsk

    Tuesday, September 6, 2016 4:00 PM
  • how do I imput a list of email into this script?

    dsk

    Use Get-Content to read in your file and use a ForEach-Object loop to process each item.

    http://ss64.com/ps/get-content.html

    http://ss64.com/ps/foreach-object.html


    Tuesday, September 6, 2016 4:05 PM
  • I want to imput the email address which is unique

    dsk

    Tuesday, September 6, 2016 4:13 PM
  • I want to imput the email address which is unique

    dsk

    http://ss64.com/ps/select-object.html


    Tuesday, September 6, 2016 4:16 PM
  • I have a list of unique email address that I need to imput via csv

    Need the script to imput the emails addresses then provide me with a list of usernames.


    dsk

    Tuesday, September 6, 2016 4:23 PM
  • Can you please simplify how to do this?

    I need to imput a csv of email addresses then then output the corresponding usernames into another csv.

    script are not my strength. 


    dsk



    • Edited by kimdav111 Tuesday, September 6, 2016 4:35 PM corection
    Tuesday, September 6, 2016 4:27 PM
  • If you're using a CSV file for input use Import-Csv instead of Get-Content.

    http://ss64.com/ps/import-csv.html

    As for simplification you'll need to explain what's confusing you. Post your script and your errors and we can help from there.


    Tuesday, September 6, 2016 4:36 PM
  • I want to imput the email address which is unique

    dsk

    You need to get the email addresses in a csv/txt file; e.g file.txt with the email column header called email

    //Import the email address

    $emails = (Import-Csv .\file.txt ).email

    Get details for each user.

    foreach($mail in $emails) {Get-ADUser -Properties mail -filter {mail -eq $mail} | | select name,mail, samaccountname >> Output.csv}

    $emails = (Import-Csv .\file.txt ).email
    
    foreach($mail in $emails) {Get-ADUser -Properties mail -filter {mail -eq $mail} | select name,mail, samaccountname >> Output.csv}

    ***PLEASE NOTE Output will need to be formatted in say excel to remove blank spaces and repeated headers


    • Proposed as answer by jrv Saturday, September 10, 2016 3:59 AM
    • Marked as answer by Richard MuellerMVP Friday, September 16, 2016 4:00 PM
    Saturday, September 10, 2016 3:50 AM