Microsoft 365 Tenant Manager
Automation for M365 Global Admins. Prefer the Microsoft.Graph module over legacy MSOnline. Test in a non-prod tenant first. Never hardcode credentials — use Key Vault or Get-Credential.
Required modules
Install-Module Microsoft.Graph -Scope CurrentUser
Install-Module ExchangeOnlineManagement -Scope CurrentUser
Install-Module MicrosoftTeams -Scope CurrentUser
Workflow 1 — New tenant setup
- Checklist + prereqs: Global Admin secured with MFA; custom domain purchased; license SKUs confirmed (E3 vs E5).
- DNS: add domain in admin center, verify MX/TXT(SPF) via
nslookup/Resolve-DnsName. Wait for propagation (up to 48h) before bulk user creation. - Security baseline: block legacy auth (Conditional Access on
exchangeActiveSync/other→ block); enable unified audit log (Set-AdminAuditLogConfig -UnifiedAuditLogIngestionEnabled $true). - Provision users from CSV with
New-MgUser+Set-MgUserLicense, wrapped in try/catch, forcing password change. - Validate: spot-check 3-5 accounts show "Active" license in portal.
Workflow 2 — Security hardening
- Audit:
Connect-MgGraph -Scopes "Directory.Read.All","Policy.Read.All","AuditLog.Read.All","Reports.Read.All". Export CA policy inventory; export users without MFA (Get-MgReportAuthenticationMethodUserRegistrationDetailwhere-not IsMfaRegistered). - MFA policy — report-only first: create CA policy with
State = "enabledForReportingButNotEnforced". - Validate 48h in Entra sign-in logs, then flip
State = "enabled". - Secure Score:
Get-MgSecuritySecureScore -Top 1; work top improvement actions.
Workflow 3 — User offboarding
- Block + revoke:
Update-MgUser -AccountEnabled:$false;Invoke-MgInvalidateAllUserRefreshToken. - Preview license removal (dry-run with
-WhatIfmindset before executing). - Execute: remove licenses (
Set-MgUserLicense ... -RemoveLicenses), convert mailbox to shared (Set-Mailbox -Type Shared), remove from all groups. - Validate: account "Blocked", no licenses, mailbox "Shared".
CA policy template (MFA for admins)
$adminRoles = (Get-MgDirectoryRole | Where-Object { $_.DisplayName -match "Admin" }).Id
New-MgIdentityConditionalAccessPolicy -BodyParameter @{
DisplayName = "Require MFA for Admins"
State = "enabledForReportingButNotEnforced" # start report-only
Conditions = @{ Users = @{ IncludeRoles = $adminRoles } }
GrantControls = @{ Operator = "OR"; BuiltInControls = @("mfa") }
}
Best practices
- Start every CA policy in report-only; review sign-in logs 48h before enforcing.
- For bulk user creation, validate every entry first (
is_validtrue) before generating the script. - Use
-WhatIf/ dry-run before destructive bulk operations; include try/catch +Write-Warninglogging for audit trails. - Use separate admin accounts with PIM; apply least privilege.