Create Organization Chart from Entra Id using PowerShell & Microsoft Graph

Create Organization Chart from Entra Id using PowerShell & Microsoft Graph


Creating an organization diagram for a company has several advantages :

1. Clarity of structure : It clearly shows the hierarchy and relationships between different positions, helping to understand who is responsible for what.

2. Improved communication : With an overview of roles and responsibilities, internal communication becomes smoother and more efficient.

3. Resource identification : Employee photos help put a face to a name, which can facilitate interactions, especially in larger companies.

4. Planning and management : An organization chart helps identify staffing needs and plan recruitment or promotions.

5. Integration of new employees : New arrivals can quickly understand the company's structure and know who to contact for various questions. Generated HTML web page can be added to SharePoint company portal.


for all this, I have created a PowerShell script to automatically generate Organization Chart directly from Entra ID.


Requirements :

  • Entra id Security group with company employees inside
  • employees informations : Photo, Job title and Manager must be filled in correctly
  • Global admin rights to execute microsoft graph commands


How the script works :

  1. connection to Microsoft graph
  2. retrieve employee list from employee group id (to be supplied)
  3. browse list and draw nodes with employee photos (HTML file and photos will be saved in "C:\temp\" folder)
  4. go through the list again to link each employee with his/her manager
  5. display the result in an html file


Here is my Entra id group :

and here is the group members (fake users randomly generated) :

and here is result of script execution :

The script need to install too modules (Microsoft Graph + PSWriteHTML) only for the first time.

Here is the script :

Before executing the script, you must change $CompanyName and $groupId variables.

# -------------------------- variables to change --------------------------------------------

# Specify your company Name
$CompanyName = "GlobalITnow"

# Entra id Group that contain list of employes
$groupId = '8d297edc-79f6-422a-ae10-6916dfddf915'

# image size for every node
$NodeSize = 50

# ------------------------------------------------------------------------------------------

# Check if Microsoft Graph Module is available, install it if not
if(!(Get-Module Microsoft.Graph -ListAvailable))
{
    Install-Module Microsoft.Graph -Force
}

# Check if PSWriteHTML Module is available, install it if not
if(!(Get-Module PSWriteHTML -ListAvailable))
{
    Install-Module PSWriteHTML -Force
}

# Import Modules
Import-Module PSWriteHTML
Import-Module Microsoft.Graph.Users

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.Read.All" , "Group.Read.All" , "GroupMember.Read.All" ,  "Directory.Read.All" , "ProfilePhoto.Read.All"

# function to get Employes Manager and to avoid to get error for the top manager
function Get-Manager{
    param (
        [Parameter(Mandatory=$true)]
        [string]$UserId
    )
    try {
        $MGId = ((Get-MgUserManager -UserId $UserId -ErrorAction Stop ).Id) 
        $Manager = Get-MgUser | Where-Object Id -EQ $MGId 
        return $Manager
    }
    catch{
        return $null
    }
}


# Get list of employes from Employes group id
$Employees = Get-MgGroupMemberAsUser -GroupId $groupId | select Id, DisplayName, JobTitle , UserPrincipalName , Manager, Photo

# Header to be displayed on the top of HTML file
$Header = "`# " + $CompanyName + " Diagram :"

# Directory used to store files
$Dir = "C:\temp\" 
if((Test-Path $Dir) -eq $false)
{
    New-Item -ItemType Directory -Path $Dir -erroraction SilentlyContinue | Out-Null
}

# Create HTML File
$HTMLFile = "C:\temp\Diagram_$(Get-Date -Format 'yyyy-MM-dd HH.mm.ss').html"
New-HTML -TitleText $CompanyName -FilePath $HTMLFile  {  
        New-HTMLSection {
            New-HTMLMarkdown -Content $Header
        }
        # Specify the height of output HTML File
        New-HTMLDiagram -Height '2000px' {
    
        New-DiagramOptionsInteraction -Hover $true
        New-DiagramOptionsPhysics -Enabled $false -HierarchicalRepulsionAvoidOverlap 1 -HierarchicalRepulsionNodeDistance 200
        New-DiagramOptionsLayout -HierarchicalEnabled $true -HierarchicalDirection FromUpToDown #-RandomSeed 0
        New-DiagramOptionsLinks -ArrowsToEnabled $true -Color BlueViolet -ArrowsToType arrow -ArrowsFromEnabled $false
        
        # Draw nodes from employes list
        foreach($Employee in $Employees)
        {
            Get-MgUserPhotoContent -UserId $Employee.Id -OutFile "C:\temp\$($Employee.Id).png"
            #$Manager = Get-Manager -UserId $Employee.Id
            New-DiagramNode -Id $Employee.UserPrincipalName -Label "$($Employee.DisplayName)`r`n$($Employee.JobTitle)" -Size $NodeSize -Image ("C:\temp\$($Employee.Id).png") 
        }

        # Draw links between nodes
        foreach($Employee in $Employees)
        {
            $Manager = Get-Manager $Employee.Id
            if($Manager)
            {
                New-DiagramLink -From $Manager.UserPrincipalName -To  $Employee.UserPrincipalName
            }
        }
    }
} -ShowHTML
        


The script can be optimised, if you have some suggestion, dont hesitate to write on comments.


Thanks


Aymen EL JAZIRI

System Administrator

Mohammad Fattahi

Microsoft Exchange & Microsoft 365 Specialist | Driving Seamless Collaboration & Productivity

4 个月

Useful tips

Very informative, thank you Aymen !

Jihed Jaoidi

Technicien informatique niveau 3 M365/Azure Cloud

4 个月

De très bons conseils

要查看或添加评论,请登录

Aymen E.的更多文章