fix the error Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.

BrownMA 20 Reputation points
2025-05-22T16:11:11.0866667+00:00

my script returns the "Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'." error. how do i correct this? I am trying to export members of security groups as well as nested groups and their members.

Define the path to export the CSV file

$csvPath = "C:\temp\SecGroups.csv"

Define the security groups to export

$securityGroups = @("Group1", "Group2", "Group3") # Replace with your security group names

Initialize an array to store the results

$results = @()

Function to get group members recursively

function Get-GroupMembers {

param (

    [string]$groupName

)

# Get the group members

$members = Get-ADGroupMember -Identity $groupName -Recursive

foreach ($member in $members) {

    # Check if the member is a group

    if ($member.objectClass -eq "group") {

        # Recursively get the members of the nested group

        Get-GroupMembers -groupName $member.SamAccountName

    } else {

        # Add the member to the results array

        $results += [PSCustomObject]@{

            GroupName      = $groupName

            SamAccountName = $member.SamAccountName

            Name           = $member.Name

            ObjectClass    = $member.objectClass

        }

    }

}

}

Loop through each security group and get its members

foreach ($group in $securityGroups) {

Get-GroupMembers -groupName $group

}

Export the results to a CSV file

$results | Export-Csv -Path $csvPath -NoTypeInformation

Write-Output "Export completed. The CSV file is located at $csvPath"

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 36,401 Reputation points
    2025-05-22T16:41:05.67+00:00

    Looks like a scope issue with the results variable. Try the append like this.

    $script:results += [PSCustomObject]@{
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.