安装 PowerShell 7 后,您仍然可以通过运行之前的版本来运行 PowerShell 脚本。如果您想要在 PowerShell 7 环境中使用先前版本的 PowerShell,则必须显式地调用这些版本。
以下是在 PowerShell 7 中运行 PowerShell 5.1 的示例代码:
Enable-WindowsOptionalFeature -Online -FeatureName MicrosoftWindowsPowerShellV5 -All -NoRestart
powershell.exe -version 5.1 -file "C:\scripts\MyScript.ps1"
同样地,如果您希望在 PowerShell 6 中运行 PowerShell 5.1,则可以使用以下命令:
Enable-WindowsOptionalFeature -Online -FeatureName MicrosoftWindowsPowerShellV5 -All -NoRestart
& "$env:windir\System32\WindowsPowerShell\v1.0\powershell.exe" -file "C:\scripts\MyScript.ps1"
请注意,要在 PowerShell 7 中运行 PowerShell 5.1 或其他先前版本的 PowerShell,必须在安装 PowerShell 7 之前安装所需的 Windows 功能。
Enable-WindowsOptionalFeature -Online -FeatureName MicrosoftWindowsPowerShellV5
Enable-WindowsOptionalFeature -Online -FeatureName MicrosoftWindowsPowerShellV4
上一篇:安装5.0.1版本的keys.yaml和config.yaml文件丢失。
下一篇:安装@agm/core和@types/google后,出现“命名空间'google.maps'没有导出成员'MouseEvent'”的错误。