As a Microsoft 365, Entra ID, and Exchange administrator, connecting to ExchangeOnlineManagement, Microsoft.Graph, and on-premises Exchange sessions multiple times a day quickly becomes repetitive. By customizing your PowerShell Profile ($PROFILE), you can automatically load your favorite helper functions, aliases, TLS settings, and a tenant-aware prompt every time you open a terminal.
In this updated guide, we show how PowerShell profile paths work across PowerShell 7 (pwsh.exe) and Windows PowerShell 5.1 (powershell.exe), and share a production-ready M365 Admin $PROFILE template.
PowerShell 7 vs. Windows PowerShell 5.1 Profile Locations
Step 1: Create and Open Your PowerShell Profile
Run this command in PowerShell 7 (or Windows PowerShell 5.1) to create your profile file if it does not exist yet and open it in VS Code or Notepad:
if (-not (Test-Path -Path $PROFILE)) {
New-Item -ItemType File -Path $PROFILE -Force | Out-Null
}
notepad $PROFILE
Step 2: Add the M365 & Exchange Admin Profile Template
Paste the following configuration into your $PROFILE and save the file:
# =====================================================================
# LAB 365 - Microsoft 365 & Exchange Administrator PowerShell Profile
# =====================================================================
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# 1. Quick Connect Helper for Exchange Online & Microsoft Graph
function Connect-M365Admin {
param(
[string]$UserPrincipalName = "[email protected]"
)
Write-Host "[*] Connecting to Exchange Online REST v3..." -ForegroundColor Cyan
Connect-ExchangeOnline -UserPrincipalName $UserPrincipalName -ShowBanner:$false
Write-Host "[*] Connecting to Microsoft Graph..." -ForegroundColor Cyan
Connect-MgGraph -Scopes "User.Read.All", "Group.Read.All", "Directory.Read.All", "AuditLog.Read.All" -NoWelcome
Write-Host "[+] Connected to Exchange Online & Microsoft Graph!" -ForegroundColor Green
}
# 2. Quick Disconnect Helper
function Disconnect-M365Admin {
Disconnect-ExchangeOnline -Confirm:$false -ErrorAction SilentlyContinue
Disconnect-MgGraph -ErrorAction SilentlyContinue | Out-Null
Write-Host "[+] Disconnected from all Microsoft 365 sessions." -ForegroundColor Yellow
}
# 3. Tenant-Aware Prompt (Shows active Graph Tenant ID & PowerShell Version)
function Prompt {
$PSVer = "PS $($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor)"
$MgCtx = if (Get-Command Get-MgContext -ErrorAction SilentlyContinue) { Get-MgContext } else { $null }
$TenantBadge = if ($MgCtx -and $MgCtx.TenantId) { " [Graph:$($MgCtx.TenantId.Substring(0,8))]" } else { "" }
Write-Host "[$PSVer$TenantBadge] " -NoNewline -ForegroundColor Cyan
Write-Host "$($executionContext.SessionState.Path.CurrentLocation)" -NoNewline -ForegroundColor Green
return "> "
}
Set-Alias -Name cm365 -Value Connect-M365Admin
Set-Alias -Name dm365 -Value Disconnect-M365Admin
Write-Host "LAB 365 Admin Profile Loaded. Type 'cm365' to connect or 'dm365' to disconnect." -ForegroundColor Cyan
Reload your profile immediately without restarting the console:
. $PROFILE
For full details on upgrading legacy Windows PowerShell 5.1 scripts to PowerShell 7 and the Microsoft Graph SDK, see our Migrating PowerShell 5.1 to 7 & Microsoft Graph Guide.
💬 Reader Comments & Feedback
Did this runbook help, or did Microsoft change a cmdlet or UI menu? Share your feedback below.
Leave a Comment or Report an Update