Skip to content

Instantly share code, notes, and snippets.

@SweetAsNZ
Last active February 5, 2022 21:51
Show Gist options
  • Save SweetAsNZ/c499b524c28233f3e57d2ec234f18f4f to your computer and use it in GitHub Desktop.
Save SweetAsNZ/c499b524c28233f3e57d2ec234f18f4f to your computer and use it in GitHub Desktop.
Get AD Departments
# https://social.technet.microsoft.com/Forums/windows/en-US/fb7ceef5-0048-4e80-826e-730cb3744c48/powershell-counting-all-ad-departments?forum=ITCG
# Get a Unique List of Departments from AD
$Output = "C:\SCRIPTS\AD\Department\Department_Count.txt"
if (!(Test-Path -Path $Output) ){
# Create a directory if it doesn't exist based on the $Output minus the file name
New-Item (Split-Path -Path $Output -Parent) -ItemType Directory
}
$Dept = Get-ADUser -Filter * -Properties Department -ResultSetSize 100000 | Where-Object {($_.Enabled -eq $true)} |
Group-Object Department | Select-Object Name,Count | Sort-Object -Descending Count,Name
$Dept | Out-File $Output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment