同时使用桌面Powershell 5.1和Powershell Core 6.1

同时使用桌面Powershell 5.1和Powershell Core 6.1,powershell,powershell-core,Powershell,Powershell Core,我在我的VS代码中玩PS.Core 6.1-使其成为默认shell,用它打开PS调试会话,打开其控制台,等等 现在,我正在打开常规桌面Powershell 5.1并获得以下信息: Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. PS C:\Users\me> get-module ModuleType Version Name

我在我的VS代码中玩PS.Core 6.1-使其成为默认shell,用它打开PS调试会话,打开其控制台,等等

现在,我正在打开常规桌面Powershell 5.1并获得以下信息:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Users\me> get-module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   6.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Script     2.0.0      PSReadline                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PS...


PS C:\Users\me> $env:PSModulePath -split ';'
C:\Users\me\Documents\PowerShell\Modules
C:\Program Files\PowerShell\Modules
c:\program files\powershell\6\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
PS C:\Users\me>
这不好。PS.Core 6.1安装了自己的
Microsoft.PowerShell.Utility
模块,该模块与桌面版本不兼容

PSModulePath环境变量现在列出了PS.Core 6模块以及桌面模块,即使优先级更高。即使我在控制面板中没有看到它:

真是一团糟

编辑1

我观察到的混乱是当我从任务栏中的快捷键启动shell时。桌面版的目标是C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe,核心版的目标是“C:\Program Files\powershell\6\pwsh.exe”-WorkingDirectory

编辑2

因此,不知何故,PS.Core的安装或我对VS.Code的实验都更新了父explorer.exe进程的环境。我在这里描述它-


可能是PS.Core安装中的一个bug,这意味着它是一次性的。或者VS代码中有一个bug,这意味着我可以再次修改它。

我认为问题可能是将PowerShell Core 6.1设置为默认shell

如果我从cmd提示符启动PowerShell 5.1(PowerShell.exe):

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

C:\>powershell.exe
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.14393.2879
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.2879
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


PS C:\> Get-Module -ListAvailable Microsoft.PowerShell.Utility


    Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Format-List, Format-Custom, Format-Table, Format-Wide...}
C:\>pwsh.exe
PowerShell 6.1.3
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.1.3
PSEdition                      Core
GitCommitId                    6.1.3
OS                             Microsoft Windows 10.0.14393
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0


PS C:\> Get-Module -ListAvailable Microsoft.PowerShell.Utility


    Directory: C:\program files\powershell\6\Modules


ModuleType Version    Name                                PSEdition ExportedCommands
---------- -------    ----                                --------- ----------------
Manifest   6.1.0.0    Microsoft.PowerShell.Utility        Core      {Format-List, Format-Custom, Format-Table, Forma...
我得到了预期的模块版本

如果我从cmd提示符启动PowerShell Core 6.1(pwsh.exe):

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

C:\>powershell.exe
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.14393.2879
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.2879
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


PS C:\> Get-Module -ListAvailable Microsoft.PowerShell.Utility


    Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Format-List, Format-Custom, Format-Table, Format-Wide...}
C:\>pwsh.exe
PowerShell 6.1.3
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.1.3
PSEdition                      Core
GitCommitId                    6.1.3
OS                             Microsoft Windows 10.0.14393
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0


PS C:\> Get-Module -ListAvailable Microsoft.PowerShell.Utility


    Directory: C:\program files\powershell\6\Modules


ModuleType Version    Name                                PSEdition ExportedCommands
---------- -------    ----                                --------- ----------------
Manifest   6.1.0.0    Microsoft.PowerShell.Utility        Core      {Format-List, Format-Custom, Format-Table, Forma...
我得到了预期的模块版本

现在,如果我首先从cmd提示符启动PowerShell Core 6.1,然后从PowerShell Core 6.1启动PowerShell 5.1(即,就像您将默认shell设置为PowerShell Core 6.1):

我列出了两个模块。当我导入模块时:

PS C:\> Import-Module Microsoft.PowerShell.Utility
PS C:\> Get-Module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   6.1.0.0    Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-Item, Clear-ItemPropert...
Manifest   6.1.0.0    Microsoft.PowerShell.Security       {ConvertFrom-SecureString, ConvertTo-SecureString, Get-Acl...
Manifest   6.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Manifest   6.1.0.0    Microsoft.WSMan.Management          {Connect-WSMan, Disable-WSManCredSSP, Disconnect-WSMan, En...
Script     2.0.0      PSReadline                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PS...
已加载较新版本(6.1)

我同意这是出乎意料的

好消息是,通过指定
-MaximumVersion
,我们也可以访问旧模块:

PS C:\> Import-Module Microsoft.PowerShell.Utility -MaximumVersion 3.1.0.0
PS C:\> Get-Module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   6.1.0.0    Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-Item, Clear-ItemProperty...}
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Script     2.0.0      PSReadline                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PSReadLineKeyHandler, Set-PSReadLineKeyHandler...}

愚蠢的问题,但你确定你要发布PowerShell 5.1吗?e、 g.通过运行
powershell.exe
。PowerShell 6.1是通过
pwsh.exe
启动的。您是否可以运行
$PSVersionTable
来验证启动的版本是否正确?屏幕截图看起来您确实要发布PowerShell 6.1。是的,我确定
$PSVersionTable.PSVersion
对于桌面返回
5.1.17763.503
,对于PS.Core返回
6.2.1
。在VS代码上设置默认值与问题无关,也与问题无关(不确定为什么会提到这一点)。我建议卸载PS 6,确保PS 5.1正常运行,然后再次尝试安装PS 6。@ShawnMelton-嗯,我只做了两件事-安装PS.Core 6.1并在VS代码中使用它。其中一个修改了父explorer.exe进程的环境,这会影响我将来从任务栏快捷方式运行的任何PowerShell。这就是我所知道的。因此,我不能排除VS代码。VS代码设置存储在settings.json文件中。它没有修改PS5.1或PS6环境变量的机制。您所说的“默认shell”是什么意思?我所观察到的混乱是当我从任务栏中的快捷方式打开powershell时。@标记是什么阻止你检查任务栏快捷方式调用的版本并插入另一个快捷方式?这就是我使用两者的方式,请记住,配置文件路径不同=>
$profile | fl*-force
@LotPings-请参见编辑2。您指的是第一项-共享
$PROFILE
?如果是,您是否建议让
$PROFILE
脚本确定powershell版本,并根据版本加载我自己的自定义脚本?单独配置文件的自定义实现?顺便说一句,CurrentUser配置文件没有区别,对于桌面版和核心版的PS,它们都是相同的路径。路径条目是一个问题,这里v5.1是第一个。但是配置文件确实不同,这里v5.1有子文件夹
WindowsPowerShell
,而v6.1.2只有
PowerShell
。您可以选择手动同步配置文件,或者通过变量
$PsEdition
$IsLinux
$IsMacOS
$IsWindows
进行区分,最后选择
$PSVersionTable.OS
。ATM我在所有平台上工作,包括Ubuntu和RaspBian。@LotPings-我非常抱歉。所有的配置文件都不同。愚蠢的我。我修改了编辑2,删除了令人尴尬的虚假陈述。我想我所需要的是再次检查我是否可以通过安装PS.Core或玩VS代码毒害explorer.exe的环境。