Crawl and Display Organization Chart using Manager and directReports field in Active Directory


If you want a quick Org Chart extract from Active Directory, you have come to right place. I wrote a script just for that. Obviously the requirement for this script are

  1. “manager” field in every user properties has to be populated
  2. Active Directory Module for Windows PowerShell has to be installed on your computer

Download below or copy/paste the script in to Notepad and save it as OrgChart.PS1. Open PowerShell go to the saved location and type ./OrgChart.ps1 <VP’s UserName>.

You may pass distinguished name of the VP’s account instead of Username. If you put your CEO’s username, you have got the whole company’s org chart. Enjoy.

 

Download it HERE or copy/paste it from below.

 

#------------------------------------------------------------------------------------------
# Function: IsAccountDisabled
# Purpose: Gets state of the Active Directory User Account (True for disabled, false for
# active)
# Parameters: <distinguishedName> 
#------------------------------------------------------------------------------------------
Function IsAccountDisabled($UserDN)
{
    #Form the LDAP URL Path
    $LDAPPath = "LDAP://$UserDN"

    #Get the ADSI object of the LDAP path
    $UserObject = [ADSI] "$LDAPPath"
    
    #Get and return Account Disabled binary value (true or false)
    return $UserObject.PsBase.InvokeGet("AccountDisabled")
}


#------------------------------------------------------------------------------------------
# Function: Get-DirectReports
# Purpose: Get the direct reports employees list. If the direct report has other
# direct reports, call this function recursily to display the direct reports.
# Parameters: <User Name> or <distinguishedName>
#------------------------------------------------------------------------------------------
function get-directreports
{
    Param($user)

    #Increase the level of organization structure by one
    #Every time this function (get-directreports) called, it is processing
    #employees from an Manager
    $level++

    #Get the User object
    $userdetails = Get-ADUser $user -Properties directReports,distinguishedName
    
    #Check the account disabled or not
    $AccountStatus = IsAccountDisabled($userdetails.distinguishedName)


    if ( $AccountStatus )
    { 
        #Yes the account disabled..no need to process.
        #skipping
    }
    else
    {
        #Processing working employee (possibly an Manager)
        #Process through all direct reports of processing employee user object
        foreach( $directreport in $userdetails.directReports )
        {
            #Check the currenly processing object is Contact or not
			$adobject = get-AdObject $directreport
            If ($adobject.ObjectClass -eq "contact")
            {
                #this current object is a contact..do nothing.
            }
            else 
            {
                #if we are in this for loop, there is/are some direct reports for
                #the processing user object

                #Store the employee level and name to the file
                "$Script:Count.  " + ("`t" * $level) + (Get-ADUser $directreport).name `
				| Out-File -FilePath .\OrgUsers.txt -Append

                #Display the employee organization level and name to the screen
                ("¦¦¦¦" * $level) + (Get-ADUser $directreport).name 

                #Count the global employee count in this organization structure
                $Script:Count++  
            
            
                #Check the each directreport employee has other directreports
                $drdetails = get-aduser $directreport -Properties directReports

         
                if ($drdetails.directReports -eq $null) 
                {
                    #No direct reports for this employee...Do Nothing
                
                }
                else
                {
                    #There are some direct reports, so call get-directreports function (itself)
                    #to process the direct reports
                    get-directreports $drdetails.distinguishedName   
                }
            }
        }
    }

    #Decrease the level of organization structure by one
    #Every time this function quits, we are going to up in the organization structure
    $level--
}





#------------------------------------------------------------------------------------------
# Name: CrawlAD.ps1
# Purpose: Get all employees working under a speific VP
# Parameters: Distinguished Name of the employee (VP or Director or Manager)
# 
# Written by: Anand Venkatachalapathy
# Written Date: May 16th 2012
#------------------------------------------------------------------------------------------

#Turning off the errors and warnings.
#I am expecting some warning on contact objects in AD and other disabled accounts.
$ErrorActionPreference = "SilentlyContinue"

#Import Active Directory Module
Import-Module ActiveDirectory

#Set the organization level to 0 mean Top of the structure.
$level = 0

#Get the passed distinguished name of the employee and assign to the vairable
$DNofVP = $args[0]

#Set the employee count to 1 of this organization
$Script:Count=1

#write to file and Display the employee number 1 of this organization
"$Script:Count.  " + (Get-ADUser $DNofVP).name | Out-File -FilePath .\OrgUsers.txt
(Get-ADUser $DNofVP).name

#Increase the employee count by 1 of this organization (before calling get-directreports
#function)
$Script:Count++

#Call the function to process the direct reports
Get-directreports $DNofVP

#Turn on displaying errors and warnings
$ErrorActionPreference = "Continue"

#--------------------------- End of Script ----------------------------------------
Advertisements

9 thoughts on “Crawl and Display Organization Chart using Manager and directReports field in Active Directory

      1. First I will say right up front. A programmer I am NOT!
        Trying to run this on a perhaps large domain – not sure what to expect when running the script. I get Loading Active Directory module for Windows Powershell with default drive ‘AD:’ on the next line [0000000 a row of “0”‘s then it goes away and back to the prompt.
        I am not certain if anything is working? Where the data might be saved after the fact. I am opening Powershell from the Start/Programs menu and moving the script from my desktop to the terminal window and then typing the top user of the domain structure as suggested to hopefully populate a full Org Chart.

  1. Hi Anand,

    First of all, thank you very much for your useful tips, they are quite handy.. 🙂

    But after editing some commands to meet my task, I really messed up my code. I want to:

    1. Get users from on OU (in AD) with their “line manager” who is one positions higher and Export to CSV file.

    2. Also, i want to add another column in CSV where the top manager will be displayed (one position lower than the Managing Director in the organization). Can you help me out with that?

    Thank you in advance!

  2. Hi Anand,

    First of all, thank you very much for your useful tips, they are quite handy.. 🙂

    But after editing some commands to meet my task, I really messed up my code. I want to:

    1. Get users from on OU (in AD) with their “line manager” who is one positions higher and Export to CSV file.

    2. Also, i want to add another column in CSV where the top manager will be displayed (one position lower than the Managing Director in the organization). Can you help me out with that?

    Thank you in advance!

    (edit)

  3. Hi Anand, great script, only drawback is that it doesn’t crawl the entire forest (we have 5 subdomains). If that’s a quick update for you, that would be great. I’m making the updates myself, which means updating all of the get-aduser commands to include -server, -searchbase, -searchscope options that point it to a GC, define the forest root and then perform a subtree search, but it’s taking me some time to get it all correctly formatted.

    Hope you’re still monitoring the replies..

    Thanks!

  4. Great Script , however I am getting names of people in O/P who have been disabled too ..
    any help is appreciated.
    Prabir

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s