0

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" } } 
1
  • 1
    If you want to use the .Count property, then you need to make sure it is an array by wrapping it in @() like if(@($GroupMembers.Member).count -eq 0). Btw. it is Member, not Members Commented May 13, 2022 at 15:28

1 Answer 1

3

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 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.