samaccountname from distingiuishedname

Hi, started learning Powershell this week and building a script to pull information off the AD.

I search an AD group for members, then need to get their manager, then the managers email.

I can get the distinguished name of the manager but for the email lookup I need their AD name.

Script so far:

$Members = Get-ADGroupMember “DDM_Users” | select-object -ExpandProperty samaccountname

$ManagerArray = @()

foreach($Member in $Members){

$ManagerDN += Get-ADUser $Member -properties manager | Select-Object -expandproperty manager
$Manager = get-aduser $ManagerDN | select-object -expandproperty samaccountname


This might look all kinds of wrong (probably does) but it’s driving me crazy and I can’t figure out how to get what I need!

Jonny, try this:
$Members = Get-ADGroupMember "DDM_Users" | select-object -ExpandProperty samaccountname

foreach($member in $members){get-aduser $member -properties * | Select-Object @{Name=“ManagerEmail”;Expression={(get-aduser -property emailaddress $_.manager).emailaddress}}}

I’ll give it a try in the morning when I’ve got the AD network back :slight_smile:

This works and obviously I can change the emailaddress field to any property I like as well! Now as I continue learning it I can understand how you did it :smiley:

Thank you very much!