Forum Discussion

Baron164's avatar
Baron164
Brass Contributor
Sep 08, 2022

Need help exporting list of enabled group members

So I have this simple one line that exports the members of a group. 

 

Get-ADgroupMember GroupName -Recursive | select Name | Sort-Object -Property Name | export-csv -Path \\domain.local\share\exportmembers.csv -NoTypeInformation

 


But I need to exclude disabled accounts so I've been trying a few things but the syntax is escaping me. I've tried this but it's not giving me the expected results.

 

Get-ADgroupMember GroupName -Recursive | Where { $_.objectClass -eq "user" } | Get-ADUser -properties * | where {$_.enabled -eq $true} | select Name | Sort-Object -Property Name | export-csv -Path \\domain.local\share\exportmembers.csv -NoTypeInformation

 


What's the best way to handle this? Any help would be greatly appreciated.


15 Replies

  • Baron164 Piping like that doesn't work (You're not using a Foreach in it), I changed it to something somewhat longer which also displays the group name in the CSV file

     

     

    $total = @()
    $group = 'Group1'
    foreach ($user in Get-ADgroupMember $group -Recursive | Where-Object objectClass -eq user ) {
        if ((Get-ADUser $user.SamAccountName).enabled -eq 'True') {
            $founduser = [PSCustomObject]@{
                Group = $group 
                User  = $user.name
            }
            $total += $founduser
        }
    }
    $total | Export-Csv -Encoding UTF8 -Path \\domain.local\share\exportmembers.csv -NoTypeInformation -Delimiter ';'

     

    • Baron164's avatar
      Baron164
      Brass Contributor

      So it's only providing me with two user accounts, which is the same that I was getting. My initial line when it's exporting everything returns over 100 users. But when I try to specify only enabled users I only get two listed. The group I'm currently trying to export from has a lot of nested groups within it.

      • (Get-ADgroupMember $group -Recursive | Where-Object objectClass -eq user).count is how many?

Resources