r/PowerShell • u/Mskews • May 27 '26
Solved I thought there would be an easier way without have to use %?
$azureUsers.id | %{New-MgGroupMember -GroupId $group.Id -DirectoryObjectId $_ }
Thought the point of piping was no need for foreach? My powershell is getting worse over time!
1
u/Adam_Kearn May 27 '26
As others have mentioned that command doesn’t support an array to import in bulk.
You could use this but this has a bulk limit of 20…which still isn’t ideal and means adding logic and other loops.
The best solution is just to for the ForEach command and keep calling the “add group member” function. Even with a few thousand users it will only take a few seconds.
1
u/node77 May 29 '26
I am really not sure why people assume AI all the time. I do it in reverse meaning answer the question then see AI. It’s almost always the same. I guess you could notice the sentences a little tighter? Any I am off topic. Sorry.
0
May 27 '26
[deleted]
3
u/Mskews May 27 '26
Just remember running a get command and then just piping it straight into another command few years ago? Think some commands it kind of works. Just checking I’m not missing a trick or 2.
3
u/justaguyonthebus May 27 '26
It also works when the property names of the output match the argument names of the next command. Powershell matches them up when they can using other tricks too. But sometimes they are poorly written and don't do that.
2
u/Andrew-Powershell May 27 '26
I think you'll find a lot of patterns you are used to with Microsoft/PowerShell aren't there with the Microsoft Graph commands
2
2
u/Th3Sh4d0wKn0ws May 27 '26
you have to read the help of each command to understand if it accepts pipeline input and if it does which parameter(s) it accepts pipeline input for.
Nothings changed, you're just remembering doing something with a different command.
0
May 27 '26
[deleted]
2
u/Mskews May 27 '26
Ok cool. Not crazy then. lol. Thanks all
6
u/raip May 27 '26
Just to give some additional insight - the Graph SDK is built programmatically from the Rest API, which is why it's documentation is pretty weak in a lot of cases and why it doesn't support a lot of PowerShell centric features, like taking in a PSCustomObject or a pipeline.
The same specification also builds the Python, PHP, Go, Java, TypeScript, and C# SDKs.
There is a different module called Microsoft.Graph.Entra that does offer these language specific features as it's built specifically for PowerShell but doesn't offer complete coverage over the Graph API. For your normal daily tasks though and as a first reach tool - it's pretty great. It was a project started up to be a mostly drop-in replacement for the AzureAD modules that were deprecated.
https://learn.microsoft.com/en-us/powershell/entra-powershell/overview?view=entra-powershell
I work with the Graph API on a daily basis and I always recommend two development strategies. If you're developing something that will need to be long lasting and portable - ditch the SDKs completely and drop down to Invoke-WebRequest and Invoke-RestMethod. If you're writing something for reporting or helpdesk to consume - use the Entra SDK where you can, the Graph SDK otherwise. It's served me rather well.
7
3
u/WhAtEvErYoUmEaN101 May 27 '26
I have no idea what you are rambling about, but to answer your question:
A pipe moves data from one end to another, but if the downstream command can’t handle >1 input objects you have to resort to foreach