Azure ad - Powershell - remove user from group a if they are a member of group b

Hello IT Professionals,

I need some help, I am trying to create some powershell script that will check if a user is a member group B and then if they are remove them from group A - Im still very new to powershell and scripting so i am having a little trouble and wondering if I could get some assistance !

Here is what i have so far and i all I can do is list the members of group B

Im not sure how i take the members object ids and then do an if found against the other group id and then remove that object id

I have tried to adapt an on premise script and im just having troubles as im new to this

Get-AzureADGroupMember -ObjectId "90e136ce-f573-4b4f9990-21a314963de2"

  # Get all members of the GroupB.

   Foreach ($ObjectId In Get-AzureADGroupMember -ObjectId "90e136ce-f573-4b4f-9990-21a314963de2")

  {

  If ((Get-AzureADGroupMember -ObjectId "746e5b45-9368-434c-bab1-5d5b7baea075" -Contains $ObjectId))

  {

  # Remove that user from GroupA

  Remove-AzureADGroupMember -ObjectId "746e5b45-9368-434c-bab1-5d5b7baea075" -Members $ObjectId

 }

 }

This was the on premise scritp i was trying to adapt.

Appreciate any help with this.

   # Get all members of the GroupB.
   Foreach ($User In Get-ADGroupMember -Identity "Group B")
   {
# If they are a 'MemberOf' GroupA
If ((Get-ADUser $User.SamAccountName -Properties MemberOf).MemberOf -Contains "Group A")
{
    # Remove that user from GroupA
    Remove-ADGroupMember -Identity "Group A" -Members $User.SamAccountName
}
 }

This was Answered for me on another forum https://www.reddit.com/r/AZURE/comments/k910l4/azure_ad_powershell_help_user_group_memberships/

Kudos to TheStig1293 on reddit

#Store the groups in a variable
$GroupA = Get-AzureADGroupMember -ObjectId '746e5b45-9368-434c-bab1-5d5b7baea075'

$GroupB = Get-AzureADGroupMember -ObjectId '90e136ce-f573-4b4f-9990-21a314963de2'

#Using Compare-object to compare the members of the groups and then using Where-object to select the ones that are in both Groups. This is stored in a variable called Dif 
$diff = Compare-Object -ReferenceObject $GroupB.ObjectID -DifferenceObject $GroupA.ObjectID -IncludeEqual | Where-Object {$_.SideIndicator -eq "=="}

#Using foreach to go through each user in diff and then removing them. We are referencing the InputObject property as the Object ID because if you look at the output of Compare-object, that is the anchor for the comparison. 

foreach($user in $diff){
    #I Included this so you can verify manually they are the users you would like to remove prior to removing. 
    #Get-AzureADUser -ObjectId $user.InputObject 
    Remove-AzureADGroupMember -ObjectID '746e5b45-9368-434c-bab1-5d5b7baea075' -MemberID ($User).InputObject
}