Please wait,
Processing your request...

    0%
  Business logo VanSoest.it
  ... | Selecteer de Nederlandse taal |
Sharing is caring
| Print this page. | Linkedin page of Johan van Soest

React: Postcard image. Click this to mail to Johan

WebHalla
 Content
  Management
   System

ICT-Hotlist Topic

PowerShell script to find all direct reports of a manager in the Active Directory.

The Microsoft Windows Active Directory (AD DS) is the standard directory for managing users, computers and security information for small businesses up to Enterprises.
In the Active Directory every user can have a manager and direct reports. The correctness of this relation is very important for workflow software that depends on getting approval from management for publishing pricelists or other sensitive information. Also ICT-departments often depend on the correct personel information in the Active Directory to get more information or authorisation for a certain employee. With all the HRM mutations it can be hard to have a correct overview of all the employees that report to a certain manager, especially when the Active Directory Users and Computers user properties dialog is to small to make a screenprint of all the direct reports.
This PowerShell script lists all the names and job titles of all the direct reports of a specified manager. Because a tab character is inserted in the output, it can be copied very easy to for example Excel.

Usage

  1. Copy the script in a Powershell window or ISE and run it.
  2. At the prompt enter the Windows logon username of the manager.
  3. Select and copy the output.

Script


###############################################################################################
# This PowerShell script finds all the direct reports of a manager in the Active Directory
# (AD DS) services. It can be run from any Active Directory connected Windows computer.
# The output is directReport's name and Title seperated with a tab so it can be copied and 
# pasted in Excel for example.
#
# (C)Copyrights 2019 - 2024 vanSoest.it by J.P.G. van Soest.
###############################################################################################

#Clear the screen using the good old DOS Command
cls
# Show some information to the user
"This PowerShell script displays all the Direct Reports of a manager"
"(C)Copyrights 2019 - 2024 vanSoest.it by J.P.G. van Soest."
# Get the manager user account
$Manager = Read-Host -Prompt 'Enter the Windows usercode of the manager'
# Try to resolve the usercode and get all the directReports
Try{
# Clear the screen using DOS Command"
    cls
# Resolve the usercode and get all the directReports
    $Users = Get-ADUser -Identity $manager -Properties directReports | Select -ExpandProperty directReports

# Get the number of results
    $NumberOfUsers = $Users | measure
# Select if there are any directReports
    If($NumberOfUsers.count -ne 0){ 
# Display the number of directReports and format output header
        $NumberOfUsers.count.ToString() + " Direct reports of manager: $Manager"
# Loop through all the directReports to get the Name and Title properties
        ForEach($User in $Users)
# There are Direct Reports, List Name and Title properties
        {
            $ADUser = Get-ADUser $User -Properties *
            $ADUser.Name + "`t" + $ADUser.Title
        }
    }
# The number of results equals 0. Display message
    Else {"No Direct reports of manager: $Manager found"}
}
Catch { "An error occurred: " +  $_.Exception.Message + "`n" + $_.Exception.ItemName } 

Example Output

Example 1

When the correct manager user code is entered at the prompt, the direct reports are listed after a clear screen.
Input
This PowerShell script displays all the Direct Reports of a manager
(C)Copyrights 2019 - 2024 vanSoest.it by J.P.G. van Soest.
Enter the Windows usercode of the manager: BrianE  
Output
4 Direct reports of manager: BrianE
JohnL	Rhythm Guitar
PaulM	Bass player
GeorgeH	Lead Guitar
RingoS	Drums

Example 2

When an incorrect user name for the manager is entered, a descriptive error message is displayed.
Input
This PowerShell script displays all the Direct Reports of a manager
(C)Copyrights 2019 - 2024 vanSoest.it by J.P.G. van Soest.
Enter the Windows usercode of the manager: AllenK  
Output
An error occurred: Cannot find an object with identity: 'AllenK' under: 'DC=vansoest,DC=local'.

Example 3

Whenever an employee is not managing people, the script will report:
Output
No Direct reports of manager: RingoS found

Requirements

PowerShell uses the Windows Active Directory libraries in this script. Each domain Controller has this installed by default. For your desktop, you may need to install or configure the Remote Server Administration Tools (RSAT). Here you can find more information.
You may vote your opinion about this article:


Scripts and programming examples disclaimer

Unless stated otherwise, the script sources and programming examples provided are copyrighted freeware. You may modify them, as long as a reference to the original code and hyperlink to the source page is included in the modified code and documentation. However, it is not allowed to publish (copies of) scripts and programming examples on your own site, blog, vlog, or distribute them on paper or any other medium, without prior written consent.
Many of the techniques used in these scripts, including but not limited to modifying the registry or system files and settings, impose a risk of rendering the Operating System inoperable and loss of data. Make sure you have verified full backups and the associated restore software available before running any script or programming example. Use these scripts and programming examples entirely at your own risk. All liability claims against the author in relation to material or non-material losses caused by the use, misuse or non-use of the information provided, or the use of incorrect or incomplete information, are excluded. All content is subject to change and provided without obligation.
Generated by WebHalla™ Version 0.1.e.7 : Saturday 27-4-2024 © Copyright 1995-2024 ing. Johan P.G. van Soest CIPM Certified Privacy Information Manager
Response Form    Cookie- and Privacy statement    Responsible Disclosure procedure
Weather in Waalre by OpenWeatherMap logo overcast clouds
Temperature 7.48 °C overcast clouds
Wind chill 6.73 °C overcast clouds
Humidity 92 % overcast clouds
Air pressure 1001 hPa overcast clouds
Wind speed 1.54 m/s overcast clouds
Wind direction South East South East overcast clouds
Sun Rise 6:17 Sun Rise
Sun Set 20:54 Sun Set
Updated:2024-04-27 07:04:52 overcast clouds

Weather Cache is 4 minute(s) old.
| Current user: Guest | Login |