question

PaulSam-0565 avatar image
0 Votes"
PaulSam-0565 asked JamesTran-MSFT edited

Powershell script to remove group owners as group members Azure AD

I have this script to get all the group owners as members on Azure AD and now I would like to remove them. Could you please guys Help? Thanks


$groups=Get-AzureADGroup -All $true
$resultsarray =@()
ForEach ($group in $groups){
$members = Get-AzureADGroupMember -ObjectId $group.ObjectId -All $true
ForEach ($member in $members){
$UserObject = new-object PSObject
$UserObject | add-member -membertype NoteProperty -name "Group Name" -Value $group.DisplayName
$UserObject | add-member -membertype NoteProperty -name "Member Name" -Value $member.DisplayName
$UserObject | add-member -membertype NoteProperty -name "ObjType" -Value $member.ObjectType
$UserObject | add-member -membertype NoteProperty -name "UserType" -Value $member.UserType
$UserObject | add-member -membertype NoteProperty -name "UserPrinicpalName" -Value $member.UserPrincipalName
$resultsarray += $UserObject
}
}
$resultsarray | Export-Csv -Encoding UTF8 -Delimiter ";" -Path "C:\test\output.csv" -NoTypeInformation

azure-ad-user-management
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

cooldadtx avatar image
1 Vote"
cooldadtx answered

If you want to remove a group member then have you looked at the Remove-AzureADGroupMember function? If it doesn't work for you then can you please post the code you're using and any errors that are occurring.

# Assuming you are storing the objectId and member Id in your data, right now you're PSObject isn't...
Remove-AzureADGroupMember -ObjectId $group.ObjectId -MemberId $member.MemberId


If you want to remove a user as owner then use Remove-AzureADGroupOwner instead.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.