AD email

Hi,
try to find user email
i have user computer name, from ad i can get user computer name:
$compname = “Test1”
$compuser = Get-ADComputer -Filter (“Name -Like ‘$compname’”) -Property * |
Select Description
$compuser

now i have First and Last Name,
How i can find email address in AD?

Thanks.

Remove " | Select Description" - it’s limiting your output.

Leverage the Get-ADUser cmdlet to get an AD user object and access the “mail” property.

$user = Get-ADUser …
$user.mail

Check out the examples at the end of the following page:
https://technet.microsoft.com/library/ee617241.aspx

Thank you both,

i try, and now i know why its not working and i need help

when i get Description from computer record, i have FirstName and LastName,
but in user account Display name set LastName, FirstName
i try : Get-ADUser -Filter {Name -like “%$compname%”} -Properties Mail | Format-List

and nothing.

Thanks

You should be able to get the same results with something like this:

get-aduser -identity USER1 -properties givenname, surname, mail | select givenname, surname, mail

Are you trying to get username or a computer name with this? %compname%…get-aduser will only retrieve users in AD
Get-ADUser -Filter {Name -like “%$compname%”} -Properties Mail | Format-List

get-aduser -identity User1-properties givenname, surname, mail | select @{e = {$_.surname};n="Lastname"}, @{e={$_.givenname};n="Firstname"}, @{e={$_.mail};n="Email"}

Lastname Firstname Email
-------- --------- -----
1        User      user1@mail.com

Hi Ertuu85,

thx, for your exp,
this in what i try to do
i have computername - Test1
i can get computer description( and this is user name),then i try find email for this user

Thx

Computers dont have email addresses assigned to them in AD

:slight_smile: i know

i have computername = Test1
i can get computer description( this is user name), and then with this Description(User Name)

i need find email for this user

Thanks

how i can can change this:

i have First and Last Name(Tom Jhons), how i can change it on Tom.Jhons@test.com

Thanks

or how i can split Tom Jhons
exp:
$compname = “Test1”
$compuser = Get-ADComputer -Filter (“Name -Like ‘$compname’”) -Property * |
Select Description
$compuser

and result: Tom Jhons

how i can split this to:

$givenname = Tom
$surname = Jhons

Thanks

This is a basic, untested example. You need to get the description, split at the spaces and then get the parts of the name:

$Description = Get-ADComputer -Filter "Name -eq 'Computer123'" -Properties Description | Select -ExpandProperty Description

if ($Description) {
    $arrDesc = $Description -split " "
    $givenname = $arrDesc[0]
    $surname = $arrDesc[1]
    #Keep in mind a search can have more than one result.  This example assumes there is on result
    $email = Get-ADUser -Filter "givenName -eq '$givenname' -and surName -eq '$surname'" -Properties mail | Select -ExpandProperty mail
}

THANK YOU Rob,

this is exactly what i was looking for

Thanks for your help.