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.

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

foreach ($Department in $Departments) {

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

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

$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

This site uses Akismet to reduce spam. Learn how your comment data is processed.