/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
$PSVersionTable.PSVersion
$PSVersionTable.PSEdition # Desktop = Windows PowerShell 5.1, Core = PowerShell 6/7Differences that affect scripts
| Area | Windows PowerShell 5.1 | PowerShell 7 |
|---|---|---|
| Executable | powershell.exe | pwsh.exe |
| Runtime | .NET Framework 4.x | .NET (Core) |
| Default file encoding | varies by cmdlet (ANSI, UTF-16LE, ASCII) | UTF-8 without BOM everywhere |
| WMI | Get-WmiObject and Get-CimInstance | Get-CimInstance only |
| Event logs | Get-EventLog and Get-WinEvent | Get-WinEvent only |
| Invoke-WebRequest | IE-based HTML parsing unless -UseBasicParsing | basic parsing only |
| Export-Csv | #TYPE line unless -NoTypeInformation | no #TYPE line |
| Profile folder | Documents\WindowsPowerShell | Documents\PowerShell |
| User module folder | Documents\WindowsPowerShell\Modules | Documents\PowerShell\Modules |
| Platforms | Windows only | Windows, Linux, macOS |
New language features in 7
$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 -UseWindowsPowerShellruns 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
- Run it under
pwshand fix errors from removed cmdlets (Get-WmiObjecttoGet-CimInstance,Get-EventLogtoGet-WinEvent). - Check files it writes: if another program expects UTF-16 or a BOM, pass
-Encodingexplicitly. - Remove dependence on
ParsedHtmlfromInvoke-WebRequest. - Update scheduled tasks and shortcuts from
powershell.exetopwsh.exe(with-NoProfile).
Sources
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/scripting/whats-new/differences-from-windows-powershell
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/scripting/whats-new/migrating-from-windows-powershell-51-to-powershell-7
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_windows_powershell_compatibility
Related pages
compare · powershellInvoke-WebRequest vs Invoke-RestMethod: which one to use in PowerShellInvoke-RestMethod parses JSON or XML into objects, made for APIs. Invoke-WebRequest returns the raw response with its status code, headers and content.flags · powershellExport-Csv -NoTypeInformation: remove the #TYPE line from CSV filesWindows PowerShell 5.1 writes a #TYPE header as the first line of Export-Csv output unless you pass -NoTypeInformation. PowerShell 6+ omits it by default.config · powershell$env:PSModulePath: where PowerShell looks for modules and how to add a folderPSModulePath lists the folders PowerShell searches to import and auto-load modules. Prepend a folder for the session, or in your profile for every session.cheat sheets · powershellPowerShell cheat sheet: files, objects, filtering, strings and error handlingPowerShell cheat sheet: find commands and help, work with files, filter and sort objects, format strings, hashtables, loops, errors and bash equivalents.