I've been trying to remove all of the groups(M365,DL,security etc.) from a user. I was trying to use this script but I'm getting errors when removing DLs(reasonably).
$Groups = Get-AzureADUserMembership -ObjectId $userID
foreach($Group in $Groups.ObjectId){
Remove-AzureADGroupMember -ObjectId $Group -MemberId $userID
}
My problem is that I have no way to get the type of the group and treat it with the correct command accordingly. When trying to use MSOL to get the type I saw that M365 groups are also being shown as a distribution list, So I'm not able to use this method.
Any advice or luck with that? Thanks!
Edit: This is how the groups are showing up, identical but not actually as it requires different command to remove the group. 365 group and DL



I tried with particular user like imran khan to remove from these two groups.


