Skip to content

/changelogs/powershell/windows-powershell-5-1-to-powershell-7

Windows PowerShell 5.1 to PowerShell 7: what changes and what breaks

The commands on this page have not been executed by our verification runner yet.updated
tool
PowerShell
versions
Windows PowerShell 5.1 → PowerShell 7.x

PowerShell 7 (pwsh.exe) is the cross-platform successor of Windows PowerShell 5.1 (powershell.exe). It runs on modern .NET instead of .NET Framework, installs side by side with 5.1 (which stays part of Windows and is not updated with new features), and has its own profile, module folder and settings. Most scripts run unchanged; what breaks is mostly encoding defaults, removed Windows-only cmdlets (WMI, workflows, EventLog) and modules that depend on .NET Framework.

Check which one you are in

powershell
$PSVersionTable.PSVersion
$PSVersionTable.PSEdition      # Desktop = Windows PowerShell 5.1, Core = PowerShell 6/7

Differences that affect scripts

AreaWindows PowerShell 5.1PowerShell 7
Executablepowershell.exepwsh.exe
Runtime.NET Framework 4.x.NET (Core)
Default file encodingvaries by cmdlet (ANSI, UTF-16LE, ASCII)UTF-8 without BOM everywhere
WMIGet-WmiObject and Get-CimInstanceGet-CimInstance only
Event logsGet-EventLog and Get-WinEventGet-WinEvent only
Invoke-WebRequestIE-based HTML parsing unless -UseBasicParsingbasic parsing only
Export-Csv#TYPE line unless -NoTypeInformationno #TYPE line
Profile folderDocuments\WindowsPowerShellDocuments\PowerShell
User module folderDocuments\WindowsPowerShell\ModulesDocuments\PowerShell\Modules
PlatformsWindows onlyWindows, Linux, macOS

New language features in 7

powershell
$name = $env:USERNAME ?? 'unknown'               # null-coalescing (7.0)
$config ??= @{}                                    # null-coalescing assignment (7.0)
$len = ${text}?.Length                             # null-conditional member access (7.1)
$mode = $IsWindows ? 'win' : 'other'               # ternary operator (7.0)
dotnet build && dotnet test                        # pipeline chain operators (7.0)
1..10 | ForEach-Object -Parallel { $_ * 2 } -ThrottleLimit 4   # parallel (7.0)

None of these parse in Windows PowerShell 5.1, so a script that uses them fails there with a parser error. Add #Requires -Version 7 at the top of such scripts to get a clear message instead.

Modules

  • Most Microsoft modules (Az, Microsoft.Graph, ExchangeOnlineManagement, MicrosoftTeams) support PowerShell 7.
  • PowerShell 7 also searches the Windows PowerShell module folders. Modules that cannot load in .NET can be loaded through the compatibility layer: Import-Module Name -UseWindowsPowerShell runs them in a hidden Windows PowerShell process and proxies the commands (returned objects are deserialized copies).
  • Some modules are still Windows PowerShell only. Check the module's documentation before moving a script.

Migrating a script

  1. Run it under pwsh and fix errors from removed cmdlets (Get-WmiObject to Get-CimInstance, Get-EventLog to Get-WinEvent).
  2. Check files it writes: if another program expects UTF-16 or a BOM, pass -Encoding explicitly.
  3. Remove dependence on ParsedHtml from Invoke-WebRequest.
  4. Update scheduled tasks and shortcuts from powershell.exe to pwsh.exe (with -NoProfile).

Sources