Help with get-aduser pipeline

This works fine when the manager is in the same domain as the user, but what if I don’t know what domain the manager will be in?

Get-ADUser $user -Properties manager | select samAccountName,@{n=‘manager’;e={(get-aduser -Properties mailnickName $_.manager).mailNickName}}

Not a whole bunch you can do about that, if you flat-out don’t know what domain they’re in at the time. You need to be able to tell the command what domain to query.

The manager field output should be a distinguished name, so you could do some string manipulation to get the domain from there. For ease of use I’d write a function called get-domainfromDn. Then your select scriptblock could be

 {get-aduser -properties mail nickname -identity $.manager - server $(get-domainfromDn $.manager) } 

Assuming your credentials are good for the other domain it should work.

Good idea… I’ll give that a whirl!


Easier solution and I hate to do it because I prefer to stay native, but I have other work to do! The Quest snap-in recognizes the domain of the object for which the property you are trying to retrieve.

get-qaduser testmtwain -service | select @{n=‘manager’;e={(get-qaduser -Properties mailnickName $_.manager).mailNickName}}