Tuesday, December 17, 2013

PowerShell - Finding Empty AD Groups



Just a quick one, if you need to find all the groups in your directory that are empty, this will do the trick:

Import-Module activedirectory
Get-ADGroup -Filter * -Properties Members | where {-not $_.members} | select Name | Export-Csv D:\emprtygroups.csv –NoTypeInformation


This uses an interesting 'trick' $_.members will return the number of members, but since zero can be interpreted as a boolean false, -not $_.members will trick the 'where' clause into only passing zero membership groups along the pipeline. I think 'trick' is a fair description, it's not evil but not very pure either. 

Cheers!

No comments:

Post a Comment