Get the managers email address

Posted On 2016-07-15 by dwirch
Tags: Powershell Scripting Active Directory Windows Windows Server 2012
Views: 1107

Sometimes, you need to tattle on a user to their manager. But how to find out who their manager is, and their email address? Here is how I did it with PowerShell.

At my job, stale user accounts are not tolerated. Inactive accounts are disabled after a period of time, with no warning. However, I wanted to at least notify the persons manager that the account was disabled, and why.

First thing you should know, this PowerShell script requires the Active Directory Module to be loaded (Import-Module ActiveDirectory).

The whole thing can be encapsulated in a one-liner, as shown below. Simply replace $UserNameHere with the name of the user. I like to use the SamAccountName for this.

$Manager=(Get-AdUser (Get-aduser $UserNameHere -properties manager).manager -properties emailaddress).EmailAddress

The downside of the one-liner is that there is no error checking. If there is no manager defined, then there is no email address to be found, resulting in a big, ugly, red error message.

To get around that fail, we can plug it into a cmdlet:

$ManagerName=(Get-ADUser $UserName -Properties Manager).manager

$ManagerEmailAddress=(Get-Aduser $ManagerName -properties EmailAddress).EmailAddress
} else {

Here, we are using a parameter called $UserName. The rest of the script is broken out to first get the managers name. If the manager field is blank, nothing is returned. If the manager field contains a value, then the script retrieves the email address, and prints it out on screen.

By piping the output of this code to the Send-MailMessage cmdlet, you can send a message to the users manager, letting him know of action taken against his employees account.

About the Author

dwirch has posted a total of 172 articles.


Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Beat With A Stick List Returns: By request, BWASL has returned to the site. Free your rage by telling the world about something that really grinds your gears. Add your beating.

Recent Forum Posts

BWASL returns
dwirch posted on May 13, 2017 at about 15:24 in Site News

BWASL returns
dwirch posted on May 13, 2017 at about 8:46 in Site News

Job Spammer: Balashankar Bose Bose
dwirch posted on May 11, 2017 at about 10:05 in Spammers

Job Spammer: Bharti Jigyasi
dwirch posted on May 11, 2017 at about 7:58 in Spammers

List of Shady Characters
dwirch posted on April 25, 2017 at about 16:39 in Webmaster Stuff

Job Spammer: Bilal Uddin
dwirch posted on April 25, 2017 at about 11:00 in Spammers