How to remove security groups

Marcio de Moraes Dourado 0 Reputation points
2023-12-08T14:08:17.1566667+00:00

Hi,

I would like to know how to remove source security groups from windows server AD in Microsoft Entra ID?

Microsoft Security | Microsoft Entra | Microsoft Entra ID
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sedat SALMAN 14,280 Reputation points MVP Volunteer Moderator
    2023-12-08T15:40:13.37+00:00

    can you be more clear?

    do you want to remove security groups from Microsoft Entra ID (that also exists in Windows server or vice versa)

    List Security Groups from Windows Server AD and remove them from Azure AD

    # Connect to Azure AD
    Connect-AzureAD
    
    # Get all group names from Windows Server AD
    $ADGroups = Get-ADGroup -Filter 'GroupCategory -eq "Security"' | Select -ExpandProperty Name
    
    foreach ($group in $ADGroups) {
        # Find the group in Azure AD
        $AzureGroup = Get-AzureADGroup -Filter "DisplayName eq '$group'" -ErrorAction SilentlyContinue
    
        # Check if the group exists in Azure AD and remove it
        if ($AzureGroup) {
            Remove-AzureADGroup -ObjectId $AzureGroup.ObjectId
            Write-Host "Group '$group' removed from Azure AD."
        } else {
            Write-Host "Group '$group' not found in Azure AD."
        }
    }
    
    

    List Security Groups from Azure AD and Remove from Windows Server AD

    # Connect to Azure AD
    Connect-AzureAD
    
    # Get all group names from Azure AD
    $AzureADGroups = Get-AzureADGroup -All $true | Where-Object { $_.SecurityEnabled -eq $true } | Select -ExpandProperty DisplayName
    
    foreach ($group in $AzureADGroups) {
        # Check if the group exists in Windows Server AD
        $ADGroup = Get-ADGroup -Filter "Name -eq '$group'" -ErrorAction SilentlyContinue
    
        # Check if the group exists in Windows Server AD and remove it
        if ($ADGroup) {
            Remove-ADGroup -Identity $ADGroup.DistinguishedName
            Write-Host "Group '$group' removed from Windows Server AD."
        } else {
            Write-Host "Group '$group' not found in Windows Server AD."
        }
    }
    
    

    if you can give more detail i can be more accurate


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.