使用 PowerShell 中的实验性功能
PowerShell 中的实验性功能支持提供了一种机制,可便于实验性功能与 PowerShell 或 PowerShell 模块中的现有稳定功能共存。
实验性功能是指设计尚未最终确定的功能。 此类功能可供用户进行测试和提供反馈。 一旦实验性功能最终确定下来,设计变更就变成了中断性变更。
注意
实验性功能不适合在生产环境中使用,因为允许变更成为中断性变更。 实验性功能不受官方支持。 不过,我们非常期望收到你的反馈和 Bug 报告。 你可以在 GitHub 源存储库中提出问题。
若要详细了解如何启用或禁用这些功能,请参阅 about_Experimental_Features。
试验性功能生命周期
Get-ExperimentalFeature cmdlet 返回 PowerShell 可用的所有试验性功能。
试验性功能可能来自模块或 PowerShell 引擎。 仅在导入模块后可使用基于模块的试验性功能。 在以下示例中,不会加载 PSDesiredStateConfiguration ,因此 PSDesiredStateConfiguration.InvokeDscResource
功能不可用。
Get-ExperimentalFeature
Name Enabled Source Description
---- ------- ------ -----------
PSCommandNotFoundSuggestion False PSEngine Recommend potential commands based on fuzzy searc…
PSCommandWithArgs False PSEngine Enable `-CommandWithArgs` parameter for pwsh
PSFeedbackProvider True PSEngine Replace the hard-coded suggestion framework with …
PSLoadAssemblyFromNativeCode False PSEngine Expose an API to allow assembly loading from nati…
PSModuleAutoLoadSkipOfflineFiles True PSEngine Module discovery will skip over files that are ma…
PSSerializeJSONLongEnumAsNumber True PSEngine Serialize enums based on long or ulong as an nume…
PSSubsystemPluginModel True PSEngine A plugin model for registering and un-registering…
使用 Enable-ExperimentalFeature 和 Disable-ExperimentalFeature cmdlet 启用或禁用某功能。 必须启动新的 PowerShell 会话才能使此更改生效。 运行以下命令以启用 PSCommandNotFoundSuggestion
功能:
Enable-ExperimentalFeature PSCommandNotFoundSuggestion
WARNING: Enabling and disabling experimental features do not take effect until next start
of PowerShell.
当某个试验性功能成为主流功能时,它不再可用作试验性功能,因为该功能已是 PowerShell 引擎或模块的一部分。 例如,如果 PSAnsiRenderingFileInfo
功能成为 PowerShell 7.3 中的主流功能。 你会自动获得该功能的所有功能。
注意
某些功能有配置要求,如首选项变量,需要将其设置为从该功能中获取所需结果。
当某试验性功能停用时,该功能在 PowerShell 中不再可用。 例如,PowerShell 7.3 中停用了 PSNativePSPathResolution
功能。
可用功能
本文介绍了可用的实验性功能及其用法。
图例
- 图标指示试验性功能在该 PowerShell 版本中可用
- 图标指示以实验性功能作为主要功能的 PowerShell 版本
- 图标指示删除了试验性功能的 PowerShell 版本
PSCommandNotFoundSuggestion
注意
此功能在 PowerShell 7.5-preview.5 中成为主流。
在 CommandNotFoundException 后根据模糊匹配搜索来建议可能命令。
PS> get
get: The term 'get' isn't recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
Suggestion [4,General]: The most similar commands are: set, del, ft, gal, gbp, gc, gci,
gcm, gdr, gcs.
PSCommandWithArgs
注意
此功能在 PowerShell 7.5-preview.5 中成为主流。
此功能为 -CommandWithArgs
启用 pwsh
形参。 通过此形参,可使用实参执行 PowerShell 命令。 与 -Command
不同,此形参填充可供命令使用的 $args
内置变量。
第一个字符串是命令,后面用空格分隔的字符串是实参。
例如:
pwsh -CommandWithArgs '$args | % { "arg: $_" }' arg1 arg2
该示例产生下面的输出:
arg: arg1
arg: arg2
此功能已添加到 PowerShell 7.4-preview.2 中。
PSDesiredStateConfiguration.InvokeDscResource
支持在非 Windows 系统上编译为 MOF,并支持在没有 LCM 的情况下使用 Invoke-DSCResource
。
从 PowerShell 7.2 开始,PSDesiredStateConfiguration 模块已删除,此功能默认处于禁用状态。 若要启用此功能,必须从 PowerShell 库安装 PSDesiredStateConfiguration v2.0.5 模块,并启用此功能。
DSC v3 没有此实验性功能。 DSC v3 仅支持 Invoke-DSCResource
,不使用或支持 MOF 编译。 有关详细信息,请参阅 PowerShell Desired State Configuration v3。
PSFeedbackProvider
启用此功能后,PowerShell 会在找不到命令时使用新反馈提供程序提供反馈。 反馈提供程序是可扩展的,并且可由第三方模块实现。 其他子系统(如预测器子系统)可以使用反馈提供程序提供预测 IntelliSense 结果。
此功能包括两个内置反馈提供程序:
GeneralCommandErrorFeedback 提供目前存在的相同建议功能
Linux 上的 UnixCommandNotFound 提供类似于 bash 的反馈。
UnixCommandNotFound 同时充当反馈提供程序和预测器。 command-not-found 命令的建议既用于在交互式运行中找不到命令时提供反馈,也用于为下一个命令行提供预测 IntelliSense 结果。
此功能是在 PowerShell 7.4-preview.3 中添加的。
PSLoadAssemblyFromNativeCode
公开 API 以便能从本机代码加载程序集。
PSModuleAutoLoadSkipOfflineFiles
注意
此功能在 PowerShell 7.5-preview.5 中成为主流。
启用此功能后,如果用户的 PSModulePath 包含来自云提供商(如 OneDrive)的文件夹,PowerShell 将不再触发该文件夹中包含的所有文件的下载。 将跳过标记为“未下载”的任何文件。 使用云提供商在计算机之间同步模块的用户应将模块文件夹标记为“已固定”或 OneDrive 以外的提供商的等效状态。 将模块文件夹标记为“已固定”可确保文件始终保留在磁盘上。
此功能是在 PowerShell 7.4-preview.1 中添加的。
PSRedirectToVariable
注意
此实验性功能是在 PowerShell 7.5-preview.4 中添加的。
启用后,此功能添加了对重定向到变量驱动器的支持。 此功能允许使用 variable:name
语法将数据重定向到变量。 PowerShell 检查重定向的目标,如果它使用变量提供程序,则会调用 Set-Variable
,而不是 Out-File
。
以下示例展示了如何将命令的输出重定向到变量:
. {
"Output 1"
Write-Warning "Warning, Warning!"
"Output 2"
} 3> variable:warnings
$warnings
Output 1
Output 2
WARNING: Warning, Warning!
PSSubsystemPluginModel
此功能在 PowerShell 中启用子系统插件模型。 利用此功能,可以将 System.Management.Automation.dll
的组件分隔到驻留在自己的程序集中的各个子系统。 这种隔离可减少核心 PowerShell 引擎的磁盘占用,并支持这些组件成为最小 PowerShell 安装的可选功能。
目前仅支持 CommandPredictor 子系统。 该子系统与 PSReadLine 模块一起使用,以提供自定义预测插件。 将来 ,Job、CommandCompleter、Remoting 和其他组件可以分隔到 外的子系统程序集。
试验性功能包括新的 cmdlet Get-PSSubsystem。 只有启用此功能时,此 cmdlet 才可用。 此 cmdlet 将返回有关系统上可用子系统的信息。
PSNativeWindowsTildeExpansion
启用此功能后,PowerShell 会在调用本机命令之前将未加引号的波浪号 (~
) 展开到用户的当前主文件夹。 下面的示例演示了该功能的工作原理。
禁用该功能后,波浪号将作为文本字符串传递到本机命令。
PS> cmd.exe /c echo ~
~
启用该功能后,PowerShell 将展开波浪号,然后再将其传递给本机命令。
PS> cmd.exe /c echo ~
C:\Users\username
此功能仅适用于 Windows。 在非 Windows 平台上,波浪号展开在本机完成。
此功能是在 PowerShell 7.5-preview.2 中添加的。
PSSerializeJSONLongEnumAsNumber
此功能使 cmdlet ConvertTo-Json 能够将基于 Int64/long
或 UInt64/ulong
的任何枚举值序列化为数值,而不是该枚举值的字符串表示形式。 这使枚举序列化的行为与其他枚举基类型的行为保持一致,其中 cmdlet 将枚举序列化为其数值。 使用 EnumsAsStrings 参数序列化为字符串表示形式。
例如:
# PSSerializeJSONLongEnumAsNumber disabled
@{
Key = [System.Management.Automation.Tracing.PowerShellTraceKeywords]::Cmdlets
} | ConvertTo-Json
# { "Key": "Cmdlets" }
# PSSerializeJSONLongEnumAsNumber enabled
@{
Key = [System.Management.Automation.Tracing.PowerShellTraceKeywords]::Cmdlets
} | ConvertTo-Json
# { "Key": 32 }
# -EnumsAsStrings to revert back to the old behaviour
@{
Key = [System.Management.Automation.Tracing.PowerShellTraceKeywords]::Cmdlets
} | ConvertTo-Json -EnumsAsStrings
# { "Key": "Cmdlets" }