Microsoft Is Cutting Windows PowerShell 5.1 Loose From the Graph SDK

The access review export doesn't arrive Monday morning, and when you run the task manually it dies with an assembly-loading error you can't force your way past. Here's what breaks when Graph SDK support…

The report doesn’t run. That’s how you find out.

It’s a Monday, the access-review export that lands in the compliance team’s inbox every week simply isn’t there, and someone pings you at 9:15 asking where it went. You open the scheduled task, click Run, and watch it fail in real time. The error isn’t a permissions problem or an expired secret. It’s uglier and more fundamental than that:

notes.txtText
Import-Module: Could not load type 'System.Runtime.CompilerServices.Unsafe'
from assembly 'System.Runtime.CompilerServices.Unsafe, Version=...'.
Assembly with same name is already loaded.

If you’ve run Microsoft.Graph on Windows PowerShell 5.1 for any length of time, you already know this error. It’s the one that shows up when two modules each drag in a different version of the same .NET assembly, and the runtime, having loaded one already, refuses to load the other. There is no clever -Force that fixes it. The engine is holding a part it won’t put down.

This is the failure Microsoft has now decided to stop patching around. Windows PowerShell 5.1 support in the Microsoft Graph PowerShell SDK is being retired. No firm date has been given, and I’ll come back to why that vagueness is its own hazard, but the direction is set. Let me walk through exactly what breaks, why, and how to make sure the failing scheduled task on some future Monday isn’t yours.

Why 5.1 was always the wrong engine for this car

Here’s the analogy I keep reaching for. Windows PowerShell 5.1 is a beautifully engineered engine built for one specific fuel: the .NET Framework, the version that shipped inside Windows and gets updated on Windows’ schedule. PowerShell 7 runs on .NET (formerly .NET Core), which is cross-platform, side-by-side, and moves fast.

The Graph SDK is a large, fast-moving car. It’s really a thousand generated cmdlets sitting on top of the Graph API, and it pulls in a stack of modern .NET dependencies to do its job, auth libraries, JSON serialisers, HTTP handlers. Those dependencies expect the new fuel.

For years Microsoft kept the old engine running on the new fuel with careful shimming. The assembly-loading conflicts were the visible symptom of that mismatch: 5.1 has a single shared load context, so if anything in your session, another module, a profile script, a leftover Az assembly, already loaded a given DLL, Graph couldn’t load its own version. PowerShell 7 has assembly load contexts that isolate this. The new engine simply doesn’t have the problem.

So the retirement isn’t Microsoft being capricious. It’s the maintenance team looking at the shim, looking at the bug reports, and deciding the effort is better spent elsewhere. Maintenance burden, dependency conflicts, assembly-loading challenges, that’s the stated reasoning, and having debugged that exact error at 11 p.m., I believe every word of it.

What actually stops working

Be clear about the blast radius, because it’s easy to over- or under-read it.

What breaks: the Microsoft.Graph module (and the SDK sub-modules like Microsoft.Graph.Authentication) loading inside Windows PowerShell 5.1. Once support is enforced, Connect-MgGraph won’t authenticate, and every downstream cmdlet, user and group provisioning, Conditional Access policy updates, sign-in and audit reporting, license assignment, goes down with it, because they all sit on that one authentication module. It’s not a graceful degradation. The import fails, and nothing built on it runs.

What does not break: the Graph API itself is untouched. Azure CLI is untouched. Your Logic Apps, your Power Automate flows, anything calling Graph over HTTPS directly, all fine. This is specifically about the PowerShell SDK refusing to load in the 5.1 runtime. Same cmdlets, same API, wrong engine.

The nasty part is the “no firm date.” You can’t put a reminder in your calendar for a deadline that doesn’t exist. It’ll arrive as a module version bump, you’ll do a routine Update-Module Microsoft.Graph, or an Automation account will pull the latest, and the new build simply won’t support 5.1 anymore. If your CI has ever auto-updated a module, you know exactly how that morning goes.

Find every place 5.1 is still driving

You can’t migrate what you can’t see. Start with the running session, then hunt the automation. These are read-only; run them and read before you touch anything.

The first check is the one everyone knows and half of us forget:

audit.ps1PowerShell
# Am I even in the right engine right now?
$PSVersionTable.PSVersion
# 5.1.x  = Windows PowerShell (at risk)
# 7.x    = PowerShell 7 (good)
$PSVersionTable.PSEdition
# Desktop = 5.1 ; Core = 7.x

Now the part that actually finds the landmines, scheduled tasks. A task calling powershell.exe runs on 5.1; one calling pwsh.exe runs on 7. Enumerate them and flag the legacy engine:

audit.ps1PowerShell
# Audit scheduled tasks for the engine they invoke. Read-only.
$results = foreach ($task in Get-ScheduledTask) {
    foreach ($action in $task.Actions) {
        $exe = ($action.Execute | Out-String).Trim()
        if ($exe -match 'powershell(\.exe)?$|pwsh(\.exe)?$') {
            [pscustomobject]@{
                TaskName = $task.TaskName
                Path     = $task.TaskPath
                Engine   = if ($exe -match 'pwsh') { 'PowerShell 7' } else { 'Windows PowerShell 5.1' }
                Command  = $exe
                Args     = ($action.Arguments | Out-String).Trim()
            }
        }
    }
}
​
$results | Where-Object Engine -eq 'Windows PowerShell 5.1' |
    Sort-Object Path, TaskName | Format-Table -AutoSize
​
# Keep the evidence for the migration ticket
$results | Export-Csv -Path .\ps51-task-audit.csv -NoTypeInformation

Then grep your script estate for anything importing or connecting to Graph, so you know which files need to run under 7:

audit.ps1PowerShell
# Find scripts that touch the Graph SDK, so you know what to retest.
$scriptRoot = 'C:\Automation'   # point at your real script/runbook folder
Get-ChildItem -Path $scriptRoot -Recurse -Include *.ps1, *.psm1 -ErrorAction SilentlyContinue |
    Select-String -Pattern 'Import-Module\s+Microsoft\.Graph|Connect-MgGraph|Mg[A-Z]\w+' |
    Group-Object Path |
    Select-Object @{n='Script';e={$_.Name}}, @{n='Hits';e={$_.Count}} |
    Sort-Object Hits -Descending

Don’t forget the estate you don’t log into by hand: Azure Automation runbooks (check whether they’re set to the 5.1 or 7 runtime version), any Hybrid Runbook Workers, build agents, and that one jump box in the corner that everyone forgot owns three critical tasks.

Migrate the engine, not the car

Here’s the genuinely good news, and the reason this is a migration and not a rewrite. The cmdlets don’t change. Get-MgUser, New-MgGroup, Update-MgIdentityConditionalAccessPolicy, identical syntax, identical parameters, same behaviour. You are swapping the engine under the same car. In most scripts the only mechanical change is the shebang and the invocation: pwsh instead of powershell.exe.

Install PowerShell 7 side by side, it does not remove or replace 5.1, they coexist happily, then install the SDK into the 7 scope:

audit.ps1PowerShell
# Run inside PowerShell 7 (pwsh)
Install-Module Microsoft.Graph -Scope CurrentUser -Repository PSGallery
​
# Sanity check that the SDK now loads cleanly in the new engine
try {
    Import-Module Microsoft.Graph.Authentication -ErrorAction Stop
    Connect-MgGraph -Scopes 'User.Read.All' -NoWelcome -ErrorAction Stop
    Get-MgContext | Format-List Account, TenantId, Scopes
}
catch {
    Write-Error "Graph SDK failed to load or connect under PS7: $($_.Exception.Message)"
}
finally {
    if (Get-MgContext) { Disconnect-MgGraph | Out-Null }
}

Test against reality before you cut over, and never let a state-changing script run for the first time without a dry run. Pipe reviewed input in, and keep -WhatIf on by default:

audit.ps1PowerShell
# Dry-run pattern: operate on a reviewed CSV, not a live query.
# Nothing is modified while -WhatIf is present.
$targets = Import-Csv -Path .\reviewed-users.csv   # you eyeballed this file
​
Connect-MgGraph -Scopes 'User.ReadWrite.All' -NoWelcome
try {
    foreach ($u in $targets) {
        Update-MgUser -UserId $u.UserId -Department $u.Department -WhatIf
    }
}
catch {
    Write-Error "Update failed for $($u.UserId): $($_.Exception.Message)"
}
finally {
    Disconnect-MgGraph | Out-Null
}
# Remove -WhatIf only after the output shows exactly the changes you expect.

For paginated reads, let the SDK do the work with -All rather than hand-rolling $top loops, it handles the @odata.nextLink paging for you:

audit.ps1PowerShell
$allUsers = Get-MgUser -All -Property Id, DisplayName, UserPrincipalName, AccountEnabled
"Retrieved $($allUsers.Count) users" | Write-Host

Run old and new side by side for a cycle or two. Point the 5.1 task and a cloned 7 task at the same job, compare outputs, and only then repoint the schedule to pwsh and disable the old one. Keep the disabled task around for a fortnight as a rollback.

The story that started this, the missing report, the assembly error, the 9:15 ping, is the story that plays out for whoever does nothing. Pull the lessons out and it’s short:

  1. “No date” is a worse deadline than a date. It arrives silently, as a module update, on a morning of the module’s choosing. Migrate on your schedule so it never gets to pick yours.
  2. The blast radius is the auth module. Everything routes through Connect-MgGraph; when it can’t load under 5.1, provisioning, Conditional Access, licensing and reporting all stop together.
  3. This is an engine swap, not a rewrite. The cmdlets are identical. Most scripts need pwsh instead of powershell.exe and nothing more.
  4. Audit the automation you never log into. Runbooks, hybrid workers, build agents, the forgotten jump box, that’s where 5.1 hides.
  5. Prove it before you cut over. Side-by-side runs, -WhatIf on by default, reviewed input files, a disabled old task as your parachute.

The engine that got you here won’t take the new fuel. Change it while the car’s in the garage, not on the motorway.