-目录开关并非始终在Powershell中工作

Yab*_*bie 0 powershell

一起屠杀Powershell脚本,我似乎与dir(Get-ChildItem cmdlet)的-Directory开关的行为不一致。

如果我运行常规的Powershell实例并输入dir -Directory,那么它将正常工作。

但是,当我将其放在.ps1文件中并运行它时,我得到了:

Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'.

是什么赋予了?

我在带有Powershell 5.1的Windows 10上

Bac*_*its 5

如果您运行PowerShell v2.0,或者以版本2模式运行PowerShell,或者SQLPS.exe,可能会发生这种情况,但是通常会发生这种情况,因为当前上下文的提供者不支持该-Directory参数。

例如,这可以正常工作:

Set-Location C:\
Get-ChildItem -Directory C:\
Run Code Online (Sandbox Code Playgroud)

这将错误:

Set-Location HKLM:\
Get-ChildItem -Directory C:\
Run Code Online (Sandbox Code Playgroud)

因为注册表提供程序使用的命令版本有所不同。

这也会失败:

Set-Location Function:\
Get-ChildItem -Directory
Run Code Online (Sandbox Code Playgroud)

因为函数提供者是如此不同,以至于它甚至都不知道容器是什么。

请参阅Get-Help about_Providers