A cloud-based identity and access management service for securing user authentication and resource access
function RemoveAllOwnerAndMembers {
param(
[Parameter(Mandatory)]
[Guid[]]$GroupIds
)
foreach ($Id in $GroupIds) {
$Owners = Get-AzureADGroupOwner -ObjectId $Id
foreach ($Owner in $Owners) {
Remove-AzureADGroupOwner -ObjectId $Id -OwnerId $Owner.ObjectId
}
$Members = Get-AzureADGroupMember -ObjectId $Id
foreach ($Member in $Members) {
Remove-AzureADGroupMember -ObjectId $Id -MemberId $Member.ObjectId
}
}
}
function AddNewOwners {
param(
[Parameter(Mandatory)]
[Guid[]]$GroupIds,
[Parameter(Mandatory)]
[Guid[]]$NewOwnersIds
)
foreach ($Id in $GroupsIds) {
foreach ($NewOwnerId in $NewOwnersIds) {
Add-AzureADGroupOwner -ObjectId $Id -RefObjectId $NewOwnerId
}
}
}
Connect-AzureAD
# Replace values
RemoveAllOwnerAndMembers -GroupIds <guid_or_string_array>
AddNewOwners -GroupIds <guid_or_string_array> -NewOwnersIds <guid_or_string_array>
Please let us know if this answer was helpful to you. If so, please remember to mark it as the answer so that others in the community with similar questions can more easily find a solution.