AntiMalware Status

Get list of Departments and number of users per Department in PowerShell

I need to create a report from Active Directory. I need to provide list of all Departments and headcount per Department. Here is quick PowerShell that will list all Departments and provide information about how many users are in the given Department.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Clear-Host
Import-Module ActiveDirectory

$Departments = Get-ADUser -Filter * -Property Department | Select-Object -ExpandProperty Department -Unique
Write-Host List of Departments and Number of Users per Department ... -ForegroundColor Green
Write-Host

foreach ($Department in $Departments) {

Write-Host = Users in $Department => (Get-ADuser -Filter * -Property Department | Where-Object {$_.Department -like "$Department"}).count -ForegroundColor yellow
}

Write-Host
Write-Host Total Users in Active Directory : (Get-ADUSer -Filter *).count -BackgroundColor red -ForegroundColor white

Write-Host
$greenCheck = @{
Object = [Char]8730
ForegroundColor = 'Green'
NoNewLine = $true
}

Write-Host "Report on Users and Departments ... " -NoNewline
Start-Sleep -Seconds 1
Write-Host @greenCheck
Write-Host " (Completed)" -ForegroundColor red

You can add more into it if you want to send it by email or export to CSV,HTML …

Leave a Reply