Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,795 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi all,
I'm trying to find two PowerShell scripts to achieve the following:
All of the information I've found online seems to point towards removing a single user from all groups and not a defined selection.
I hope you can assist me with this requirement, please let me know if you need any further information.
Many thanks,
Alex.
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.