|
![]() |
|
|||||||
![]() |
ICT-Hotlist TopicPowerShell 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
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 - 2023 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 - 2023 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 OutputExample 1When 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 - 2023 vanSoest.it by J.P.G. van Soest. Enter the Windows usercode of the manager: BrianE 4 Direct reports of manager: BrianE JohnL Rhythm Guitar PaulM Bass player GeorgeH Lead Guitar RingoS Drums Example 2When 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 - 2023 vanSoest.it by J.P.G. van Soest. Enter the Windows usercode of the manager: AllenK An error occurred: Cannot find an object with identity: 'AllenK' under: 'DC=vansoest,DC=local'. Example 3Whenever an employee is not managing people, the script will report:Output No Direct reports of manager: RingoS found RequirementsPowerShell 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 disclaimerUnless 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. |