'Powershell : getting AD groups without members
I want to get AD groups with no members like below. How can I get this?
My desired output:
Group Name, Members
Group01 , YES
GRoup02 , NO
Here is my script :
$groups = Import-Csv -Path "C:\temp\unused groups\unused.csv"
foreach ($group in $groups) {
$GroupMembers = Get-ADGroup -Identity $($group.Name) -Properties Members
if(($GroupMembers.Members).count -eq 0)
{
Write-host "TRUE"
}
else{
Write-host "FALSE"
}
}
Solution 1:[1]
It's unclear if you're looking to export the result to CSV or simply display the object to the console, in case it's the latter just remove the Export-Csv
part.
Import-Csv -Path "C:\temp\unused groups\unused.csv" | ForEach-Object {
$out = @{ 'Group Name' = $_.Name }
if((Get-ADGroup $_.Name -Properties Member).Member) {
$out['Members'] = 'YES'
}
else {
$out['Members'] = 'NO'
}
[pscustomobject] $out
} | Export-Csv path\to\export.csv -NoTypeInformation
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Santiago Squarzon |