`PowerShellVersion` 和 `PowerShellHostVersion` 的区别

Difference between `PowerShellVersion` and `PowerShellHostVersion`

总结

PowerShell 模块清单值 PowerShellVersionPowerShellHostVersion 之间有什么区别?

长版

创建新模块清单时,会设置此模块所需的最低 PowerShell 版本以及所需的 PowerShell 主机最低版本。即

New-ModuleManifest -Path '.\MyModule.psd1' -PowerShellVersion '5.0' -PowerShellHostVersion '2.0'

PowerShellVersion$PSVersionTable.PSVersion.Major 相关(注意:与主要版本相关,因为有效值都为其次要版本设置了 0 / 没有构建或修订号)。

PowerShellHostVersion 是我不清楚的那个。我认为这与 $Host.Version 相关(即 PowerShellHostName$Host.Name 相关)。然而,根据我的经验,ISE (Windows PowerShell ISE Host) 和 ConsoleHost 的版本号都与 PS 版本一致;因此要求这些与示例中暗示的 PS 版本不同步似乎很奇怪。我希望这些是罕见用例中很少使用的参数;但我想确保我已正确理解它们的用途,以及它们是否适用于常见场景。

MSDN's documentation 中的示例值不同(即 PowershellVersion 给出 5.0,而 PowershellHostVersion 给出 2.0

Official Documentation只是给出了一个循环描述(即添加的信息不超过参数名称本身所暗示的信息)。

My belief is that this relates to $Host.Version

你的想法是正确的。通过修改生成的清单以将 PowerShellHostVersion 的值设置为 6.0 来对此进行测试。导入的时候报错:

Import-Module : The current Windows PowerShell host is: 'ConsoleHost' (version
5.1.15063.674). The module 'C:\MyModule.psd1' requires a minimum Windows PowerShell
host version of '6.0' to run.
At line:1 char:1
+ Import-Module .\MyModule.psd1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (C:\MyModule.psd1:String) [Import-M
   odule], InvalidOperationException
    + FullyQualifiedErrorId : Modules_InsufficientPowerShellHostVersion,Microsoft.PowerShel
   l.Commands.ImportModuleCommand

版本与 $Host.Version 相关,名称与 $Host.Name

相关
PS C:\> $Host.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      15063  674

This Shavy Levy blog post and the linked PowerShell Team follow up 从 2010 年开始讲述一段有趣的历史。在我的机器上,我从控制台和 ISE 主机获得相同的 $Host.Version 输出。
不知道 Power GUI 和其他提到的主机使用什么版本。