Export csv of groups that are not members of some groups

Im exporting some users and groups that "are not member of". But Im having troubles with groups, I did it with users but I don't know how to do the same with group.

In this example I export users that are not members of that group.

$groups = 'GG_LCS_UsersType4', 'GG_LCS_UsersType3', 'GG_LCS_UsersType2', 'GG_LCS_SpecialUsers'
$whereFilter = $groups | Foreach-Object { 
    $g = (Get-ADGroup -server $domain $_).DistinguishedName
    "{0} '{1}'" -f '$_.memberOf -notcontains',$g 
}
$whereFilter = [scriptblock]::Create($whereFilter -join " -and ")
$users = (Get-ADUser -server $Domain -filter {objectclass -eq "user"} -properties memberof).where($whereFilter)
$users | Select-Object SamAccountName,Enabled |
    Export-Csv "${Domain}_Users_withoutGG.csv" -NoTypeInformation -Encoding UTF8 -Append

So I want the groups too. Could you help me please?

Thank you!

Upvotes: 0

Views: 96

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25001

Technically, you should be able to just add a Get-ADGroup command at the end of the posted code and then export the desired data.

$FilteredGroups = (Get-ADGroup -Server $Domain -Filter * -Properties MemberOf).where($whereFilter)
$FilteredGroups | Select-Object SamAccountName |
    Export-Csv "groups.csv" -NoTypeInformation -Encoding UTF8 -Append

Upvotes: 1

Related Questions