Home > Enterprise >  How can I pass a specific item property to a variable in PowerShell?
How can I pass a specific item property to a variable in PowerShell?

Time:10-28

I'm currently working on a script to manage Azure AD (as opposed to the console GUI) and am having issues at one particular part. I'm trying to do an 'add user to group' module, but with Add-AzureADGroupMember requiring the group ObjectId and not display name, it's not initially user-friendly.

Here's what I tried initially:

>>     $UPN = "[email protected]"
>>     $Selected = "Group Display Name"
>>     $Group = Get-AzureADGroup -Filter "DisplayName eq '$Selected'" -All $true | Select-Object -Property ObjectID
>>     Add-AzureADGroupMember -ObjectID $Group -RefObjectID $UPN

The problem I have with this, is that $Group is returning '@{ObjectId=fba435cc-913c-46a0-9932-17c01733e143}' as opposed to '{fba435cc-913c-46a0-9932-17c01733e143}'

Is there a better way I can pass the group's ObjectID to a variable? I'd like for users to be able to select the display name and have the variable return the objectID.

CodePudding user response:

To get just the value of a property, use ForEach-Object -MemberName instead of Select-Object -Property:

$Group = Get-AzureADGroup -Filter "DisplayName eq '$Selected'" -All $true | ForEach-Object -MemberName ObjectID
  • Related