Home > Mobile >  Removing all AzureAD group memberships for a user
Removing all AzureAD group memberships for a user

Time:06-10

So I work in a hybrid enviroment. I have a script that I put together that will strip all their memberships in AD and I want to add onto it the ability to do the same for AzureAD.

Get-AzureADUserMembership -ObjectId $user | foreach-object {Remove-AzureADGroupMember -MemberId $user -ObjectId $_.ObjectID}

This snippet is causing me problems. It tries to do the foreach but it doesn't seem able to get the $_.objectID from the UserMembership cmdlet so it errors out.

Remove-AzureADGroupMember : Error occurred while executing RemoveGroupMember 
Code: Request_UnsupportedQuery
Message: Unsupported referenced-object resource identifier for link property 'members'.
RequestId: b1975588-c678-4183-b037-a52f8772b08f
DateTimeStamp: Thu, 09 Jun 2022 00:02:42 GMT
HttpStatusCode: BadRequest
HttpStatusDescription: Bad Request
HttpResponseStatus: Completed

I tried to seperate it out and do

$groups=Get-AzureADUserMembership -ObjectId $user
$groups | foreach-object {Remove-AzureADGroupMember -MemberId $user -ObjectId $groups.ObjectID}

And it gave me this error

Remove-AzureADGroupMember : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ObjectId'. Specified method is not supported.

I am still pretty new to powershell so any help would be appreciated.

CodePudding user response:

I tried to run the same script in my environment and got same error as below:

enter image description here

As suggested by Santiago Squarzon in the comments, when I modified the code like below, got response successfully:

$user = 'Your_user_objectid' 
$groups=Get-AzureADUserMembership -ObjectId $user
$groups | foreach-object {Remove-AzureADGroupMember -MemberId $user -ObjectId $_.ObjectID}

Response:

enter image description here

  • Related