PowerShell从5.1升级到7.X

文章目录

  • 环境
  • 背景
  • [安装PowerShell 7.X](#安装PowerShell 7.X)
  • 其它
    • [启动PowerShell 5.1和7.X](#启动PowerShell 5.1和7.X)
    • [PSVersionTable.PSVersion](#PSVersionTable.PSVersion)
    • [启动PowerShell 5.1时强制启动7.X](#启动PowerShell 5.1时强制启动7.X)
  • 参考

环境

  • Windows 11 专业版

背景

PowerShell 5.1是Windows内置的,发布时间是2016 年。现在PowerShell版本已经到了7.5.1,不过需要手工安装。

另外,由于兼容性(模块兼容性和.NET依赖)的问题,安装PowerShell 7.X并不会覆盖5.1,二者可以同时并存。

  • PowerShell 5.1的位置是 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
  • PowerShell 7.X的位置是 C:\Program Files\PowerShell\7\pwsh.exe

安装PowerShell 7.X

打开 https://github.com/PowerShell/PowerShell/releases ,我看到的最新版本是v7.5.1,在Assets下找到合适的操作系统。注意,默认只列出了前面一部分,需要点击 Show all 28 assets 才会全部列出来:

我下载的文件是 PowerShell-7.5.1-win-x86.exe

下载后双击安装,安装过程略。

其它

启动PowerShell 5.1和7.X

如上所述,PowerShell 5.1和7.X的安装位置不同,文件名也不同,所以二者可以并存。

  • 启动PowerShell 5.1: powershell
  • 启动Powershell 7.X: pwsh

注:安装Powershell 7.X时,会自动添加路径,而Powershell 5.1本来就在路径里。

$PSVersionTable.PSVersion

可以用 $PSVersionTable.PSVersion 命令查看当前shell的版本:

  • Powershell 5.1:
powershell 复制代码
PS C:\Users\Administrator> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      22621  2506
  • Powershell 7.X:
powershell 复制代码
PS C:\Users\Administrator> $PSVersionTable.PSVersion

Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
7      5      1

启动PowerShell 5.1时强制启动7.X

如果想要在运行PowerShell 5.1时也强制运行7.X,则可以通过修改配置文件来达到目的。

首先定位配置文件:

powershell 复制代码
PS C:\Users\Administrator> echo $profile
C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

注:在Powershell 5.1或者7.X下,其 $profile 是一样的。

编辑这个文件(如果不存在则创建之):

powershell 复制代码
if ($PSVersionTable.PSVersion.Major -lt 7) {
    pwsh
    exit
}

逻辑很简单,检查版本号,如果小于7,则启动 pwsh (即Powershell 7.X),并结束自身。

现在,启动Powershell 5.1(运行 powershell 或者从开始菜单选中 Windows PowerShell ),报错了:

powershell 复制代码
. : 无法加载文件 C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1,因为在此系统上禁
止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 3
+ . 'C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.Power ...
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [],PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

这个报错与系统安装策略有关。要解决这个问题,需要调整执行策略以允许运行脚本。

首先查看当前策略:

powershell 复制代码
PS C:\Users\Administrator> Get-ExecutionPolicy -List

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser       Undefined
 LocalMachine       Undefined

CurrentUser 的策略调整为 RemoteSigned

powershell 复制代码
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned -Force

验证修改:

powershell 复制代码
PS C:\Users\Administrator> Get-ExecutionPolicy -List

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser    RemoteSigned
 LocalMachine       Undefined

再次启动PowerShell 5.1:

powershell 复制代码
Windows PowerShell
版权所有(C) Microsoft Corporation。保留所有权利。

安装最新的 PowerShell,了解新功能和改进!https://aka.ms/PSWindows

PowerShell 7.5.1
PS C:\Users\Administrator>

可见,成功启动了PowerShell 7.X。可以用 $PSVersionTable.PSVersion 来验证版本。

参考

  • https://github.com/PowerShell/PowerShell/releases
相关推荐
路由侠内网穿透2 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
研华嵌入式2 天前
如何在高通跃龙QCS6490 Arm架构上使用Windows 11 IoT企业版?
arm开发·windows·嵌入式硬件
带娃的IT创业者2 天前
Windows 平台上基于 MCP 构建“文心一言+彩云天气”服务实战
人工智能·windows·文心一言·mcp
csdn_aspnet2 天前
Windows Node.js 安装及环境配置详细教程
windows·node.js
摇滚侠2 天前
java语言中,list<String>转成字符串,逗号分割;List<Integer>转字符串,逗号分割
java·windows·list
Source.Liu2 天前
【Pywinauto库】12.2 pywinauto.element_info 后端内部实施模块
windows·python·自动化
Source.Liu3 天前
【Pywinauto库】12.1 pywinauto.backend 后端内部实施模块
开发语言·windows·python·自动化
私人珍藏库3 天前
[Windows] FileOptimizer v17.1.0_一款文件批量压缩工具
windows·批量压缩
掘根3 天前
【CMake】List
windows·microsoft·list
TToolss3 天前
删除文件夹里的网盘图标
windows