Finding empty groups in Active Directory

The PowerShell function discussed in this section helps you to find out the groups that have no members in them. This function has an optional switch parameter called -Nested, which indicates that a group has to be queried recursively for membership to determine whether it is empty or not. In some cases, a group can have another group in it, which might be empty as well. This switch will come in handy to find such cases:

Function Find-EmptyADGroups { [CmdletBinding()] Param( [switch]$Nested ) $Groups = Get-ADGroup -Filter * Write-Host "`nBelow is the list of empty groups in Active Directory`n`n" $Count = 0 foreach($Group in $Groups) { $Members = Get-ADGroupMember -Identity $Group -Recursive:$Nested if(!$Members) ...

Get Active Directory with PowerShell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.