为什么PowerShell ISE中的控制台不使用最新安装的PowerShell版本?

5
我最近安装了PowerShell 6.2。
如果我启动PowerShell 6(x64)命令提示符并运行$PSVersionTable.PSVersion,则会得到以下结果。
Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
6      2      0

从同一个提示符中,我使用powershell_ise.exe运行ISE,然后PowerShell ISE启动。然而,在ISE内的控制台中,如果我运行$PSVersionTable.PSVersion,它会报告如下:

Major  Minor  Build  Revision
-----  -----  -----  --------
4      0      -1     -1  

有没有可以控制ISE搜索PowerShell的设置?或者有没有任何方法确保它正在使用已安装的最新版本?

更新: 安装PowerShell Core(即版本6.2)的一部分是我必须安装Windows Management Framework 5.1。根据this doc,我的理解是这应该升级ISE控制台的PowerShell版本到5.1。但是,如上所述,我仍然看到4.0版本。我错过了什么?


2
请在此处查找答案:https://dev59.com/rVQK5IYBdhLWcg3wGMMP - 2 B
5个回答

7
最新版本的PowerShell是5.1,这也是你可以在ISE中使用的最新版本。
PowerShell 6也被称为PowerShell Core,在ISE中不受支持。你可以下载一个名为Visual Studio Code的工具,用于与PowerShell 6(Core)一起使用。
奖励: 有趣的是,我最近读到了一篇关于Microsoft目前正在开发的PowerShell 7的文章,看起来非常有趣。此外,请参见here有关PowerShell 7的信息。
更新:感谢@Magnetron在评论中的更新。PowerShell 7本周正式发布。
希望这可以帮助你!

谢谢@cet51,这很有帮助。我可能会在现实中使用熟悉的VSCode。然而,我正在使用的Microsoft实验室使用ISE。如果我安装5.1,控制台会自动使用它吗?对于入门学习来说,如果可以避免偏离脚本,通常会很有帮助。 - Trevor Reid
回答自己的评论@cet51,如果我理解正确,升级到PS 5.1并没有被控制台注意到。我更新了问题。 - Trevor Reid
1
这确实很奇怪。当您升级Windows管理框架时,它也会升级Windows Powershell,我曾以为它也会升级ISE版本。 - cet51
1
更新:本周发布了 PowerShell 7(https://devblogs.microsoft.com/powershell/announcing-powershell-7-0/)。 - Magnetron


2
我使用以下链接向Powershell ISE添加了一个插件,可以让你在Powershell 5和6之间切换(请参见“PowerShell ISE Add-On Command”)。然而,当你关闭Powershell ISE并打开一个新会话时,你必须再次运行脚本,否则选项“Add-ons”将不会出现。我猜在发布Powershell 7时可以使用相同的过程。 在Windows PowerShell ISE中使用PowerShell Core 6和7

2

0

针对那些想要一个简短版本的启用此功能的人。

在 ISE 中运行此命令(取自其他答案中的链接)

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Clear()
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Switch to PowerShell 7", { 
        function New-OutOfProcRunspace {
            param($ProcessId)

            $ci = New-Object -TypeName System.Management.Automation.Runspaces.NamedPipeConnectionInfo -ArgumentList @($ProcessId)
            $tt = [System.Management.Automation.Runspaces.TypeTable]::LoadDefaultTypeFiles()

            $Runspace = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($ci, $Host, $tt)

            $Runspace.Open()
            $Runspace
        }

        $PowerShell = Start-Process PWSH -ArgumentList @("-NoExit") -PassThru -WindowStyle Hidden
        $Runspace = New-OutOfProcRunspace -ProcessId $PowerShell.Id
        $Host.PushRunspace($Runspace)
}, "ALT+F5") | Out-Null

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Switch to Windows PowerShell", { 
    $Host.PopRunspace()

    $Child = Get-CimInstance -ClassName win32_process | where {$_.ParentProcessId -eq $Pid}
    $Child | ForEach-Object { Stop-Process -Id $_.ProcessId }

}, "ALT+F6") | Out-Null

然后重新启动您的 ISE 并转到位于 File、Edit、View 等旁边的 Add-ons 按钮。现在应该有一个切换到 Powershell 7 的选项。

就这样!只需要一分钟。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接