Skip to content
All articles
Automation 12 min read15 Jul 2026

PowerShell Automation & Scripting for Enterprise IT

Production PowerShell automation: best practices, Exchange Online, Azure Automation runbooks, Active Directory and reporting with the Microsoft Graph SDK.

913480111119101114831041011081083444346512211711410132651171161111099711610511111034443477105991141111151111021163271114971121043444346599116105118101326810511410199116111114121344434651171161111099711610511111034935154538498103
Illustrated preview for: PowerShell Automation & Scripting for Enterprise IT
PowerShell automation pipelineA scheduled trigger runs an Azure Automation runbook that authenticates with a managed identity to Microsoft Graph, Azure and Key Vault, then writes a structured result.ScheduletriggerRunbookidempotent logicMicrosoft GraphAzureKey VaultStructured logauditable
Architecture diagram — original to this article.

Treating PowerShell like production code

PowerShell automation fails in production for predictable reasons, and none of them are PowerShell bugs: errors are silently swallowed, scripts depend on deprecated modules, credentials are stored in plaintext, and non-idempotent logic creates duplicates on re-run. The fix is a shift in mindset — treat a runbook like production code, with structured error handling, managed identities, idempotent logic, and version control. A script that works once is fast to write; a runbook that runs unattended for years is engineered.

The single highest-impact change is setting $ErrorActionPreference = "Stop" at the top of every module. Without it, cmdlets throw non-terminating errors that never enter a catch block, so a failed step is silently skipped and the runbook reports success while doing nothing. Combined with structured logging of every caught exception, this turns silent failures into loud, auditable ones.

The Microsoft Graph migration: off deprecated modules

The MSOnline and AzureAD PowerShell modules are deprecated and will stop working against Microsoft Graph. Scripts that still call Connect-MsolService or Connect-AzureAD are a ticking time bomb — they will fail with no code change on your side when Microsoft retires the endpoints. The migration path is the Microsoft Graph PowerShell SDK, where most cmdlets have a direct equivalent.

Audit your scripts for the deprecated modules now and migrate before the retirement forces an emergency rewrite. For unattended runbooks, use app-only certificate authentication against Graph rather than a stored password, and consent to the least-privilege application permissions the runbook actually needs.

Deprecated cmdletMicrosoft Graph equivalentNotes
Get-MsolUserGet-MgUserUse -Filter for server-side filtering
Get-AzureADGroupGet-MgGroupConsent to Group.Read.All
Set-MsolUserLicenseSet-MgUserLicenseGroup-based licensing preferred
Connect-MsolServiceConnect-MgGraphApp-only cert for runbooks

Credentials, managed identities, and Key Vault

A runbook that stores a service-account password in plaintext or in a weakly protected variable is a security incident waiting to happen. If the runbook is exfiltrated or the automation account compromised, the credential is exposed. The modern pattern is a managed identity in Azure Automation: the runbook authenticates to Graph, Azure, and Key Vault using the automation account's identity, with no password anywhere in the code or configuration.

Where a managed identity is not possible — for example a runbook that must reach a third-party system — store the secret in Azure Key Vault and retrieve it at runtime with the managed identity. Never hardcode or store plaintext credentials in a runbook, and rotate any service-account credentials on a schedule rather than only after an incident.

  • Use a managed identity in Azure Automation — no stored passwords
  • Store any required secrets in Key Vault and retrieve at runtime
  • Use app-only certificate auth for unattended Graph access
  • Rotate service-account credentials on a schedule, not on incident

Idempotency: safe to re-run, safe to retry

A scheduled runbook re-runs, and a failed runbook is retried, so every operation must be idempotent — running it twice for the same input must produce the same result as running it once, not a duplicate. A joiner-mover-leaver script that creates a user, assigns licences, and opens a ticket will create duplicates if it runs twice for the same person. The fix is a guard clause at the top of each operation: if the target state already exists, skip creation.

Idempotent scripts are safe to re-run, safe to retry after a failure, and safe to schedule. Non-idempotent scripts are none of these, and they are the most common cause of duplicate objects and inconsistent state after a runbook failure and retry. Always run a -WhatIf review before any bulk destructive operation so you can verify the scope before it commits.

PowerShell — Idempotent guard before creating a user
function New-ProvisionedUser {
  param([string]$GivenName, [string]$Surname)
  $upn = ("{0}.{1}@corp.contoso.com" -f $GivenName, $Surname).ToLower()
  # Idempotency guard — skip if the user already exists
  if (Get-MgUser -Filter "userPrincipalName eq '$upn'" -ErrorAction SilentlyContinue) {
    Write-Verbose "User $upn exists — skipping"
    return
  }
  New-MgUser -DisplayName "$GivenName $Surname" -UserPrincipalName $upn -AccountEnabled
}

Written by

Jamshed Alam

Global MSP & IT Infrastructure Consultant with 20 years across Exchange, Microsoft 365, Azure, security and automation. Available for project and contract work worldwide.

Share
JA

Jamshed Alam

Global MSP | IT Infrastructure Consultant | Cloud & Automation Expert · Lahore, Pakistan

EmailLinkedInUpworkGuru© 2026 Jamshed Alam