← Back to articles M365

Automating Microsoft 365 Employee Offboarding: Shared Mailboxes, Token Revocation & Intune License Reclaim

Automating Microsoft 365 Employee Offboarding: Shared Mailboxes, Token Revocation & Intune License Reclaim

Employee offboarding is one of the most critical security and operational procedures in enterprise IT. When executed manually across fragmented consoles (Entra ID, Exchange Online, and Intune), administrators frequently leave residual access: active mobile device sessions, unrevoked OAuth refresh tokens, and costly stranded licenses consuming monthly budget.

To eliminate security blindspots, organizations must deploy a fully automated, scripted offboarding pipeline. This guide provides a battle-tested PowerShell and Microsoft Graph automation script that executes all critical termination steps in sequence within seconds.

🛑 Account Lock & Tokens AccountEnabled = False Revoke-MgUserSign ✉️ Mailbox Conversion Set-Mailbox -Type Shared Delegate to Manager 💻 Intune Device Action Retire / Remote Wipe Corporate Data Purge 💰 License Reclaim E5 / F3 / Copilot Licenses Cost Savings Realized FIGURE 1: AUTOMATED M365 OFFBOARDING WORKFLOW
Figure 1: End-to-End Automated Employee Offboarding Lifecycle.

Production PowerShell & Graph Offboarding Script

# ==============================================================================
# Enterprise M365 Employee Offboarding Script
# ==============================================================================
[CmdletBinding()]
param(
    [Parameter(Mandatory = $true)]
    [string]$UserPrincipalName,

    [Parameter(Mandatory = $true)]
    [string]$ManagerUPN
)

Write-Host ">>> [1/5] Disabling Account & Revoking Active Sessions..." -ForegroundColor Cyan
Update-MgUser -UserId $UserPrincipalName -AccountEnabled:$false
Revoke-MgUserSignInSession -UserId $UserPrincipalName | Out-Null
Write-Host "Tokens invalidated and account disabled." -ForegroundColor Green

Write-Host ">>> [2/5] Converting Mailbox to Shared & Assigning Delegation..." -ForegroundColor Cyan
Set-Mailbox -Identity $UserPrincipalName -Type Shared
Add-MailboxPermission -Identity $UserPrincipalName -User $ManagerUPN -AccessRights FullAccess -InheritanceType All -AutoMapping $true
Set-Mailbox -Identity $UserPrincipalName -HiddenFromAddressListsEnabled $true
Write-Host "Mailbox converted to Shared and delegated to $ManagerUPN." -ForegroundColor Green

Write-Host ">>> [3/5] Setting Out of Office Auto-Reply..." -ForegroundColor Cyan
Set-MailboxAutoReplyConfiguration -Identity $UserPrincipalName -AutoReplyState Enabled -InternalMessage "This employee has left the company. Please contact $ManagerUPN for assistance." -ExternalMessage "This contact is no longer with the organization. Please reach out to $ManagerUPN."

Write-Host ">>> [4/5] Initiating Intune Corporate Retire on Managed Devices..." -ForegroundColor Cyan
$devices = Get-MgUserManagedDevice -UserId $UserPrincipalName
foreach ($dev in $devices) {
    Write-Host "Retiring Intune Device: $($dev.DeviceName) ($($dev.Id))..." -ForegroundColor Yellow
    Invoke-MgRetireDeviceManagementManagedDevice -ManagedDeviceId $dev.Id
}

Write-Host ">>> [5/5] Reclaiming M365 & Intune Licenses (Retaining Data in Shared Mailbox)..." -ForegroundColor Cyan
# Shared Mailboxes without In-Place Archive only require a license if >50GB
$assignedLicenses = (Get-MgUser -UserId $UserPrincipalName -Property AssignedLicenses).AssignedLicenses
$removeSkuIds = $assignedLicenses.SkuId
if ($removeSkuIds) {
    Set-MgUserLicense -UserId $UserPrincipalName -AddLicenses @() -RemoveLicenses $removeSkuIds | Out-Null
    Write-Host "Reclaimed $($removeSkuIds.Count) licenses." -ForegroundColor Green
}
Write-Host ">>> OFFBOARDING COMPLETED FOR $UserPrincipalName" -ForegroundColor Green

Summary & Implementation Checklist for Engineers

  1. Validate Manager Relationship: Ensure the manager UPN is verified before assigning mailbox delegation.
  2. Execute Mailbox Conversion: Always convert to Shared BEFORE removing the Exchange Online license to prevent mailbox deletion.
  3. Revoke Refresh Tokens: Enforce Revoke-MgUserSignInSession to terminate mobile Outlook and Teams sessions immediately.
  4. Trigger Intune Retire: Wipe corporate sandbox data from personal devices or initiate full wipe on corporate hardware.

Was this article helpful?

🎯
MSEndpoint Academy

Évaluez vos compétences Microsoft 365 & Intune (MD-102)

100% Gratuit • 5 Min

Vous appliquez ce guide en production ? Testez votre niveau technique face aux questions réelles de l'examen Microsoft 365 Certified: Endpoint Administrator (MD-102). Découvrez vos points forts et vos faiblesses immédiatement.

💡 Mini-Challenge Express Question 1 sur 10

Quel outil est obligatoire pour convertir une application Win32 (.exe) au format requis (.intunewin) pour son déploiement via Microsoft Intune ?

🔒 0€ Débité 📊 Scorecard instantanée 🤖 Explications IA
Passer le Test Diagnostic Complet (10 Questions)

🎓 Ready to go deeper?

Practice real MD-102 exam questions, get AI feedback on your weak areas, and fast-track your Intune certification.

Start Free Practice → Book a Session
Souhaiel Morhag
Souhaiel Morhag
Microsoft Endpoint & Modern Workplace Engineer

Souhaiel Morhag is a Microsoft Intune and endpoint management specialist with hands-on experience deploying and securing enterprise environments across Microsoft 365. He founded MSEndpoint.com to share practical, real-world guides for IT admins navigating Microsoft technologies — and built the MSEndpoint Academy at app.msendpoint.com/academy, a dedicated learning platform for professionals preparing for the MD-102 (Microsoft 365 Endpoint Administrator) certification. Through in-depth articles and AI-powered practice exams, Souhaiel helps IT teams move faster and certify with confidence.

Related Articles

Popular on MSEndpoint