$AllGroups=Get-adgroup -filter *| select *
$Distributions = $AllGroups | where{ $_.GroupCategory -eq "Distribution"}
$Security = $AllGroups | where{ $_.GroupCategory -eq "Security"}
#Members
Write-Host "Total Distribution Groups $($Distributions.Count)"
foreach($group in $Distributions){
Write-Host "Working with group $($group.Name)" -ForegroundColor Magenta
$allmembers =Get-adGroupMember $($group.SamAccountName)
$users = $allmembers | where{ $_.ObjectClass -eq "User"}
$Groups = $allmembers | where { $_.ObjectClass -eq "Group"}
Write-Host "Total Users in Group $($users.Count)"
$users | select Name,SamAccountname
if($Groups){
Write-Host "Total Groups in Group $($Groups.Count)"
$Groups | select DisplayName,SamAccountname
}
else{
Write-Host "There are no groups within the current group"
}
}
Write-Host "Total Security Groups $($Security.Count)"
foreach($group in $Security){
Write-Host "Working with group $($group.Name)" -ForegroundColor Magenta
$allmembers =Get-adGroupMember $($group.SamAccountName)
$users = $allmembers | where{ $_.ObjectClass -eq "User"}
$Groups = $allmembers | where { $_.ObjectClass -eq "Group"}
Write-Host "Total Users in Group $($users.Count)"
$users | select Name,SamAccountname
if($Groups){
Write-Host "Total Groups in Group $($Groups.Count)"
$Groups | select DisplayName,SamAccountname
}
else{
Write-Host "There are no groups within the current group"
}}
Well, the 1st answer will depend on what are you planning to do with the grouping.
there's no good or bad answer there, it will depend on what you want to do with the groups.
Usually, Security groups are only for users and you use them for security access to file servers and resources.
It's not usual to get computers into groups.