Exporting AD group members

Hello I am getting this error when exporting AD group members

Error Get-ADGroupMember : A parameter cannot be found that matches parameter name ‘GroupObjectId’.
`
$Groups = Get-ADGroup -Filter {name -like “XYZ*”} -SearchBase “OU=Groups,OU=Name1, OU=Name2”
foreach ($group in $groups){
$group.DisplayName
$members = Get-ADGroupMember -GroupObjectId $group.ObjectId -All
foreach ($member in $members)
{$member | Add-Member -NotePropertyName “Group” -NotePropertyValue ($group.DisplayName)
$member | select Group, EmailAddress, DisplayName | Export-Csv ADtest1.csv -NoTypeInformation -Append }
}

Thanks for helping

 

 

Titan, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
Thanks in advance.

Normally, you find AD objects, you use -Identity to find the object. There is no -GroupObjectId parameter. Additionally, made some changes to clean the code up a bit, but did not test it:

$Groups = Get-ADGroup -Filter {name -like “XYZ*”} -SearchBase “OU=Groups,OU=Name1, OU=Name2”

$results = foreach ($group in $groups){
    $members = Get-ADGroupMember -Identity $group -All
    foreach ($member in $members) {
        $member | select @{Name='Group';Expression={$group.DisplayName}}, EmailAddress, DisplayName
    }
}

$results | Export-Csv ADtest1.csv -NoTypeInformation 

How about this, which worked for me…but I couldn’t find a switch “-All” but used “-Recursive”

$AdGroup = 'xyz*'
Get-ADGroup -Filter {name -like $AdGroup} -SearchBase 'OU=Groups,OU=Name1,DC=Myself,DC=corp' | %{
Get-ADGroupMember -Identity $_.name -Recursive | %{
Get-ADUser -Identity $_.SamAccountName -Properties * | Select Name, SamAccountName, EmailAddress | %{
[PSCustomObject] @{
Group = $Group
Name = $_.name
UserID = $_.SamAccountName
Email = $_.EmailAddress
}
} | Export-Csv D:\temp\ADtest1.csv -NoTypeInformation -Append
}
}

Thanks I’ve formatted as recommended.

Also when I run your script there is a new error

 
Get-ADGroupMember : A parameter cannot be found that matches parameter name ‘All’.

Here’s an example using multiple filters and splatting, I find it easier to read and follow along.

$params = @{
    Filter     = "name -like '*Payroll*' -or name -like '*HR*'"
    SearchBase = “OU=Groups,OU=Name1, OU=Name2”
}

$props = @(
    @{Name='Group';Expression={$group.Name}},
    "SamAccountName",
    "EmailAddress"
)

foreach($Group in (Get-ADGroup @params))
{
    Get-adgroupmember $Group |
        Where-Object objectclass -eq 'user' |
            get-aduser -Properties emailaddress | 
                select $props | Export-Csv C:\temp\ADtest1.csv -NoTypeInformation
}

CSV will have 3 colums: Group, SamAccountName, and EmailAddress

Thanks this one worked

$params = @{
    Filter     = "name -like '*Payroll*' -or name -like '*HR*'"
    SearchBase = “OU=Groups,OU=Name1, OU=Name2”
}

$props = @(
    @{Name='Group';Expression={$group.Name}},
    "SamAccountName",
    "EmailAddress"
)

foreach($Group in (Get-ADGroup @params))
{
    Get-adgroupmember $Group |
        Where-Object objectclass -eq 'user' |
            get-aduser -Properties emailaddress | 
                select $props | Export-Csv C:\temp\ADtest1.csv -NoTypeInformation
}

It is taking a long time to run thanks for your help