查看 PowerShell 版本($PSVersionTable)
确认当前运行的是哪个版本的 PowerShell(内置 5.1 还是新版 7.x),再跑版本敏感的脚本。
运行 $PSVersionTable.PSVersion,5.1 是系统内置的 Windows PowerShell,7.x 是新一代跨平台 PowerShell。
01命令
命令
$ $PSVersionTable.PSVersion02作用说明
$PSVersionTable 是自动变量,其 PSVersion 字段显示引擎版本,Edition 字段区分 Desktop(Windows PowerShell 5.1)和 Core(PowerShell 7+)。有些模块只兼容其中一种,先查清楚能避免莫名其妙的兼容报错。想体验 PowerShell 7 又不动内置版本,可用 winget install --id Microsoft.PowerShell 并排安装。
03风险
!注意: 注意区分快捷方式:"Windows PowerShell" 打开的是 5.1,而 "PowerShell 7"(pwsh)才是新引擎。
04撤销
✓无需撤销——该命令不会改变持久状态。
05来源
- about_Automatic_Variables依据: Documents $PSVersionTable, the automatic hashtable containing PSVersion, Edition (Desktop/Core) and platform details of the running engine.
- about_PowerShell_Editions依据: Explains the Desktop edition (Windows PowerShell 5.1) versus Core edition (PowerShell 7+) and their module compatibility differences.
最后验证: 2026-09-18 · 分类: 开发者