Cannot export Usermembership into a CSV-File "A positional parameter cannot be found that accepts argument "System.Object"

Bühler Gabriel 81 Reputation points
2024-01-16T11:51:35.12+00:00

Hello Everyone

I created an easy script that should just give you a CSV-File with all Group memberships of a user:

$Username = Read-Host "Please Enter your Username"
$Grouplist = Get-AzureADUser -ObjectID $username | Get-AzureADUserMembership | % {Get-AzureADObjectByObjectId -ObjectId $_.ObjectId | select DisplayName,ObjectType,MailEnabled,SecurityEnabled,ObjectId} | Export-CSV $Grouplist -Path "C:\users\GBU101\Groupinfo_$Username.csv" -NoTypeInformation -Append -Delimiter ";"

But I unfortuantely always get this error:

Export-Csv : A positional parameter cannot be found that accepts argument 'System.Object[]'.

Can you please help?

Thank you.

Kind regards,

Gabe

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,817 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
23,284 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Morten Sonne 595 Reputation points MVP
    2024-01-16T12:18:40.79+00:00

    Hi Gabe, Does this make any changes for you?

    $Username = Read-Host "Please Enter your Username"
    $Grouplist = Get-AzureADUser -ObjectID $Username | Get-AzureADUserMembership | ForEach-Object {
        Get-AzureADObjectByObjectId -ObjectId $_.ObjectId | Select-Object DisplayName, ObjectType, MailEnabled, SecurityEnabled, ObjectId
    }
    
    $Grouplist | Export-CSV -Path "C:\users\GBU101\Groupinfo_$Username.csv" -NoTypeInformation -Append -Delimiter ";"
    

    It works for me - have done some small changes but can for sure be optimized :)

    0 comments No comments

  2. Rich Matheisen 47,596 Reputation points
    2024-01-16T20:31:05.03+00:00

    There's no need for the $Grouplist variable. The error is caused by your use of $Grouplist as a positional parameter (in position zero). The -Path parameter value can be provided without naming the parameter because zero is its assigned position number. But the -Path value must be a string, not an array (which is what $Grouplist is). Since there are no other parameters to be found that accept an array, the error is thrown. You're already using pipes so, unless you need the contents of $Grouplist somewhere else in your script, this should produce the CSV without any intermediate varaibles:

    $Username = Read-Host "Please Enter your Username"
    Get-AzureADUser -ObjectId $username | 
        Get-AzureADUserMembership | 
            Get-AzureADObjectByObjectId  | 
                Select-Object DisplayName, ObjectType, MailEnabled, SecurityEnabled, ObjectId | 
                    Export-Csv -Path "C:\users\GBU101\Groupinfo_$Username.csv" -NoTypeInformation -Append -Delimiter ";"
    
    0 comments No comments

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.