Skip to content

Remove-MsOGroup

MSOnline: Remove a group from Azure AD

#Requires -Version 5.1

[CmdletBinding()]
Param(
    [Parameter(Mandatory = $true, ParameterSetName = 'Id')]
    [guid]$GroupObjectId,
    [Parameter(Mandatory = $true, ParameterSetName = 'Name')]
    [string]$GroupName,
    [Parameter(ParameterSetName = 'Name')]
    [Parameter(ParameterSetName = 'Id')]
    [guid]$TenantId
)

Process {
    try {
        if ($PSCmdlet.ParameterSetName -eq 'Id') { $grp = Get-MsolGroup -ObjectId $GroupObjectId -TenantId $TenantId -ErrorAction Stop }
        else { $grp = Get-MsolGroup -SearchString $GroupName -TenantId $TenantId -ErrorAction Stop | Select-Object -First 1 }

        $null = Remove-MsolGroup -ObjectId $grp.ObjectId -TenantId $TenantId -ErrorAction Stop

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

Unique ID of the group to remove

Display name of the group to remove

Unique ID of the tenant

An interactive directory of PowerShell scripts.