I use PowerShell regex to filter only the friendly name portion of the manager from the DN for the "Manger" attribute in the AD user object properties, see below:
$newUser = Get-ADUser -Identity someUser1 -Properties *
$newUser.Manager
Output:
CN=Some Manager1,OU=IT,DC=YOUR,DC=DOMAIN,DC=COM
$newUser.Manager.split(',')[0].trim('CN=')
Output:
Some Manager1
In addition, you could use the following filter as well, but I feel it is more code than necessary, trim does what we want with less typing and complexity (Not going into why one is better than the other, we haven't talked scale about this ask):
CN=someUser1,OU=IT,DC=YOUR,DC=DOMAIN,DC=COM
$newUser.Manager.split(',')[0] -replace 'CN=',''
Output:
Some Manager1
For completeness, you should know the type of your final state, you can get this with the following:
($newUser.Manager.split(',')[0].trim('CN=')).GetType()
OR
($newUser.Manager.split(',')[0] -replace 'CN=','').GetType()
Output:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
Now you know your final value is of type 'string'!
Lastly, you can trap your value in a variable:
$Mgr = $newUser.Manager.split(',')[0].trim('CN=')
Okay, I found a bug in my regex using trim logic, if an user has a DN that starts like the following:
CN=Nicholas
Then the output using $newUser.Manager.split(',')[0].trim('CN=') is as follows:
icholas
This is because trim truncates any matching character, not the whole string specified, where as -replace does it based on the string as shown below:
$newUser.Manager.split(',')[0] -replace 'CN=',''
Output:
Nicholas
FINAL SOLUTION: So, I recommend the following as a final solution:
$newUser.Manager.split(',')[0] -replace 'CN=',''
My apologies for the oversight, I now remember seeing others mention this online and I completely forgot about it. Once again, I apologize for the confusion.
Enjoy!
managerproperty is the distinguished name of the manager's user object - to get the manager's name, you need to find that object (bind to it) and then retrieve it's nameforeachloop