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!

1 comment:

  1. I can honestly tell you, that I have been searching literally SOLID for the past few days a solution which yours is literally what I needed.

    I've tried ForEach-Object loops, I've tried '$_.Members.Count -eq 0' and god knows how many others - You have saved me, thank you so much.

    ReplyDelete