Forum Discussion

TJCooper440's avatar
TJCooper440
Copper Contributor
Aug 26, 2024
Solved

Query OU - Get Groups - Get Group Members - Export to CSV

The AI result did not work I am getting an error. The error is "Get-ADGroupMember: The input object cannot be bound to any parameters for the command either because the command does not take pipeline...
  • Harm_Veenstra's avatar
    Aug 26, 2024

    TJCooper440 I think AI thinks that the #SearchBase starts in a Container (CN), but that's not the case, I guess. Should be $SearchBase = "Ou=Corp,DC=test,DC=local". I rewrote the script and tested it, should work like this:

     

     

    Import-Module ActiveDirectory
    
    # Specify the OU where your groups are located
    $SearchBase = "Ou=Test,DC=test,DC=loc"
    
    # Get all groups within the specified OU
    $Groups = Get-ADGroup -Filter * -SearchBase $SearchBase
    
    # Initialize $GroupMembers and loop through the groups and add the results to $groupmembers while running
    $GroupMembers = foreach ($Group in $Groups) {
        $Members = $Group | Get-ADGroupMember
        foreach ($Member in $Members) {
            [PSCustomObject]@{
                "GroupName"   = $Group.Name
                "Description" = $Group.Description
                "Member"      = $Member.Name
            }
        }
    }
    
    # Export the results to a CSV file
    $GroupMembers | Sort-Object GroupName | Export-CSV C:\temp\group_members.csv -NoTypeInformation -Delimiter ';' -Encoding UTF8

     

     

    I removed the -Properties * part, too. It is unnecessary to retrieve all groups' properties when querying members... Changed to Foreach loop to be the $GroupMembers variable and used a PSCustomObject instead of adding members and using += to add $info to the $GroupMembers variable

Resources