Please wait,
Processing your request...

    0%
  Business logo VanSoest.it mourning
  ... | Selecteer de Nederlandse taal | Print this page. | Linkedin page of Johan van Soest | User: Guest | Login

Postcard image. Click this to mail to Johan

WebHalla
 Content
  Management
   System


ICT-Hotlist Topic

Back to the ICT-Hotlist...
Don't miss latest updates Follow @WebHalla

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 - 2020 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 - 2020 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 - 2020 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 - 2020 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.5 : Friday 23-10-2020 © Copyright 1995-2020 ing. Johan P.G. van Soest CIPM Certified Privacy Information Manager
Response Form    Cookie- and Privacy statement
Weather in Waalre by OpenWeatherMap logo fog
Temperature 9.02 °C fog
Wind chill 8.46 °C fog
Humidity 100 % fog
Air pressure 1010 hPa fog
Wind speed 0.5 m/s fog
Wind direction North North fog
Sun Rise 8:16 Sun Rise
Sun Set 18:28 Sun Set
Updated:2020-10-23 07:22:03 fog

Weather Cache is 3 minute(s) old.