Skip to content

Remove-O365Group

Azure AD: Removes a group from the tenant

#Requires -Version 5.1
#Requires -Modules AzureAD

[CmdletBinding(DefaultParameterSetName = "GroupName")]
Param(
    [Parameter(Mandatory = $true, ParameterSetName = "GroupObjectId")]
    [guid]$GroupObjectId,

    [Parameter(Mandatory = $true, ParameterSetName = "GroupName")]
    [string]$GroupName
)

Process {
    try {
        if ($PSCmdlet.ParameterSetName -eq "GroupObjectId") {
            $grp = Get-AzureADGroup -ObjectId $GroupObjectId -ErrorAction Stop
        }
        else {
            $grp = Get-AzureADGroup -All $true -ErrorAction Stop | Where-Object { $_.Displayname -eq $GroupName }
        }

        if ($null -eq $grp) { throw "Group not found" }

        $null = Remove-AzureADGroup -ObjectId $grp.ObjectId -ErrorAction Stop

        [PSCustomObject]@{
            Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
            Status    = "Success"
            GroupName = $grp.DisplayName
            ObjectId  = $grp.ObjectId
            Message   = "Group '$($grp.DisplayName)' removed"
        }
    }
    catch { throw }
}

Unique object ID of the group to remove

Display name of the group to remove

An interactive directory of PowerShell scripts.