2

I've searched all over and I think this would be fairly common but maybe not.

I want to list the domain of a user with the username via Get-ADGroupMember. So for instance I have this:

Get-ADGroupMember -identity "MyGroup" -Recursive -Server "my.domain.net" | select-object SamAccountName

which lists

SamAccountName
--------------
jsmith
dsmith
lsmith

I want that list to return the users primary domain so for instance I have the domains

prod.my.domain.net
dev.my.domain.net
my.domain.net

I want a query that will return

prod\jsmith
dev\dsmith
my\lsmith

depending on the users domain (note order of names does not matter).

2 Answers 2

3

Ok after googling more and playing with this for a while I came up with the following which gets what I need. I'm not sure if this is the best way to get it, but it seems clean cut to me

Get-ADGroupMember -identity "MyGroup" -Recursive -Server "my.domain.net" | select-object @{name="Login"; expression={(($_.DistinguishedName  -split '(DC=)')[2] -replace ',')+"\"+$_.SamAccountName}}

which gives

Login
-----
prod\jsmith
dev\dsmith
my\lsmith
1
  • This seems like a direct and elegant solution.
    – uSlackr
    Jan 18, 2017 at 20:44
0

I do not have a multi-domain environment to test on, but I believe this will do what you want.

Get-ADGroupMember -Identity 'MyGroup' | foreach {
$ADuser = Get-ADUser -Identity $_.sAMAccountName -Properties UserPrincipalName
$Domain = $ADuser.UserPrincipalName.Split('@.')[1]
$Name = $Domain + '\' + $ADuser.Name
$Name
}
1
  • This is a nice script, but may not be authoritative because users can have multiple valid UPNs, even in a single domain single forest environment. Also, Element Zero is looking for a string based on NetBIOSDomainName\sAMAccountName, but the base UPN suffix is based on the DNS domain name, which can be different from the NetBIOS domain name. While this query will most likely return the original domain name's UPN suffix instead of alternate UPN suffixes, I'm not sure that you can guarantee it will always return the UPN suffix that matches the NetBIOS domain name of the user's primary domain.
    – Sam Erde
    Jan 18, 2017 at 18:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .