fortypoundhead.com

Get the managers email address

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


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:

[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$UserName
)
$ManagerName=(Get-ADUser $UserName -Properties Manager).manager

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

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 180 articles.

You can find more information from dwirch by visiting http://www.derekwirch.com.


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:54.81.131.189

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




Recent Forum Posts

Advanced search added
dwirch posted on September 23, 2017 at about 13:44 in Site News

Job Spammer: Gaurav Mehta - AgreeYa Solutions
dwirch posted on September 22, 2017 at about 10:35 in Spammers

Job Spammer: Prutha Siri - Javelin Systems
dwirch posted on September 10, 2017 at about 6:15 in Spammers

New security implemented
dwirch posted on September 7, 2017 at about 7:16 in Site News

Malicious IP Checker Companion Tool
dwirch posted on August 12, 2017 at about 20:24 in Site News

Job Spammer: Steve Adams
dwirch posted on August 8, 2017 at about 7:44 in Spammers