对 PowerShell Core 脚本使用 .pwsh 而不是 .ps1 扩展名?

Use .pwsh instead of .ps1 extension for PowerShell Core scripts?

我同时使用 PowerShellPowerShell Core。 并非我为 "big" PowerShell 编写的所有脚本都适用于 PowerShell Core,反之亦然。

为了避免混乱,我在考虑是否应该使用两个文件扩展名:

Windows 中扩展比在 Unix 系统中更重要 shebang (#! /bin/mytool) 将有助于解决问题。

是使用两个扩展"best practice"还是有更好的选择?

附加:我不确定当我从命令行/终端调用脚本时,PowerShell 是否会执行 .pwsh 脚本。

我在 Unix / Linux 上下文中发现了类似的问题。 https://unix.stackexchange.com/questions/182882/use-sh-or-bash-extension-for-bash-scripts

添加一行

#requires -PSEdition Core

在您的 PowerShell Core 脚本的开头,以及一行

#requires -PSEdition Desktop

在常规 PowerShell 脚本的开头。

对于两个版本中 运行 的脚本,只需省略该行。

来自documentation

-PSEdition <PSEdition-Name>

Specifies a PowerShell edition that the script requires. Valid values are Core for PowerShell Core and Desktop for Windows PowerShell.

不要使用不同的扩展名,因为这会影响功能。例如,PowerShell 不会使用 .ps1.

以外的扩展名点源脚本