HOW TO FIND AND REPORT NESTED GROUPS IN ACTIVE DIRECTORY USING POWERSHELL

Since I started working with and learning Powershell, I try to find solution of complicated tasks which seems to be simple when we put in one Sentence.
Well, one way is to double click a group and check the members tab and enter in Excel rows and columns. If I have nothing else to do and I love clicking mouse buttons then I guess that will be a nice time killer job. or else find a 3rd party software a freeware or shareware with limitations that does not really most of the time gives what I need.

Fortunately my favorite is PowerShell !.,

Here is the result
image

$Report = @()
$GroupCollection = Get-ADGroup -Filter * | select Name,MemberOf,ObjectClass,SAMAccountName

Foreach($Group in $GroupCollection){
$MemberGroup = Get-ADGroupMember -Identity $Group.SAMAccountName | where{$_.ObjectClass -eq ‘group’}
$MemberGroups = ($MemberGroup.Name) -join “`r`n”
if($MemberGroups -ne “”){
$Out = New-Object PSObject
$Out | Add-Member -MemberType noteproperty -Name ‘Group Name’ -Value $Group.Name
$Out | Add-Member -MemberType noteproperty -Name ‘Member Groups’ -Value $MemberGroups

$Report += $Out
}
}
$Report | Sort-Object Name | FT -AutoSize
$Report | Sort-Object Name | Export-Csv -Path ‘C:\Group-MemberGroups-Report.csv’ -NoTypeInformation

Hope this helps.

Don’t forget to Wrap Text the Member Groups column.

0275

Leave a Reply

Your email address will not be published. Required fields are marked *

*