Do I need ForEach-Object when piping users into New-MgGroupMember?

August 5, 2026 3 answers
Asked by MellowCedar42
I'm trying to add several users to an Azure AD group with the Microsoft Graph PowerShell cmdlet: $azureUsers.id | ForEach-Object { New-MgGroupMember -GroupId $group.Id -DirectoryObjectId $_ } I thought the purpose of piping was to pass each item directly into the next command, so I'm wondering whether there's a way to pipe the IDs straight into New-MgGroupMember without using ForEach-Object. Am I missing a simpler approach, or does this cmdlet only accept one directory object ID at a time?

Answers

SilverMaple88
The key distinction is that piping does not automatically mean โ€œexpand this array as arguments.โ€ It sends each pipeline object to a parameter only when the next command declares pipeline support for that parameter. Since this cmdlet expects a single directory object ID per invocation, the loop is required unless you build a separate Graph batch request.
Reply to this answer
PracticalLynx19
Some Microsoft Graph operations support adding multiple members through a batch request, but that introduces batching limits and extra logic. For a regular list of users, looping with ForEach-Object and calling New-MgGroupMember once per ID is straightforward and usually fast enough, even for a few thousand users.
Reply to this answer
QuietOrbit7
A pipeline passes objects one at a time, but the receiving cmdlet still has to support pipeline input. New-MgGroupMember doesnโ€™t accept an array of directory object IDs through the pipeline, so it wonโ€™t automatically create one membership per ID. In this case, your ForEach-Object is the normal approach: each ID is bound to -DirectoryObjectId and the cmdlet runs once for each user.

MellowCedar42: That makes sense. I was remembering other commands that do accept multiple pipeline objects, so I wanted to make sure I wasnโ€™t overlooking something.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions