Skip to content

Remove-Mailbox

Exchange Online: Removes a mailbox

#Requires -Version 5.1

[CmdletBinding()]
Param(
    [Parameter(Mandatory = $true)]
    [string]$MailboxId,
    [switch]$Permanent
)

Process {
    try {
        $box = Get-Mailbox -Identity $MailboxId -ErrorAction Stop | Select-Object UserPrincipalName, DisplayName
        if ($null -eq $box) {
            throw "Mailbox not found"
        }
        $null = Remove-Mailbox -Identity $box.UserPrincipalName -Permanent:$Permanent -Confirm:$false -Force -ErrorAction Stop

        [PSCustomObject]@{
            Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
            Message   = "Mailbox $($box.DisplayName) removed"
        }
    }
    catch { throw }
}

Alias, Display name, Distinguished name, SamAccountName, Guid or user principal name of the mailbox to remove

Off

Permanently removes the mailbox from the database

An interactive directory of PowerShell scripts.