Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PowerShell中获取以P开头的目录_Powershell - Fatal编程技术网

在PowerShell中获取以P开头的目录

在PowerShell中获取以P开头的目录,powershell,Powershell,我想得到所有以“p”开头的目录 在cmd中,您可以执行以下操作: dir p* 如何在Windows PowerShell中执行此操作? 我尝试了这个,但它没有返回任何结果: Get-ChildItem -Path C:\thePath\* -dir -Include p* 删除-Include p*将按预期返回所有文件夹。或者直接在-Path参数中包含模式: Get-ChildItem -Path C:\thePath\p* -Directory 。。。或者使用-Filter参数,这是更

我想得到所有以“p”开头的目录

在cmd中,您可以执行以下操作:

dir p*
如何在Windows PowerShell中执行此操作?

我尝试了这个,但它没有返回任何结果:

Get-ChildItem -Path C:\thePath\* -dir -Include p*

删除
-Include p*
将按预期返回所有文件夹。

或者直接在
-Path
参数中包含模式:

Get-ChildItem -Path C:\thePath\p* -Directory
。。。或者使用
-Filter
参数,这是更快的选择,因为它在源位置进行过滤:

Get-ChildItem -LiteralPath C:\thePath -Filter p* -Directory

至于你所尝试的:

-Include
-Exclude
参数是众所周知的违反直觉的,因为它们只在输入路径或模式本身上运行,而不在给定输入路径的子项上运行-除非还指定了
-Recurse

在您的例子中,假设您的
-Path
参数以
*
结尾,那么
-Include
过滤器应该可以工作,但由于额外存在
-Directory
开关,所以无法解释,这应该被视为一个错误。
似乎,使用
Get ChildItem
-与
Get Item
-
-相反,Include
仅包含文件,不包含目录-请参阅

您可以通过(a)从
Get ChildItem
切换到
Get Item
,以及(b)在事件发生后过滤掉非目录,使您的命令正常工作,但这比上面的替代方法效率低:

Get-Item C:\thePath\* -Include p* | Where-Object PSIsContainer

将模式直接包含在
-Path
参数中:

Get-ChildItem -Path C:\thePath\p* -Directory
。。。或者使用
-Filter
参数,这是更快的选择,因为它在源位置进行过滤:

Get-ChildItem -LiteralPath C:\thePath -Filter p* -Directory

至于你所尝试的:

-Include
-Exclude
参数是众所周知的违反直觉的,因为它们只在输入路径或模式本身上运行,而不在给定输入路径的子项上运行-除非还指定了
-Recurse

在您的例子中,假设您的
-Path
参数以
*
结尾,那么
-Include
过滤器应该可以工作,但由于额外存在
-Directory
开关,所以无法解释,这应该被视为一个错误。
似乎,使用
Get ChildItem
-与
Get Item
-
-相反,Include
仅包含文件,不包含目录-请参阅

您可以通过(a)从
Get ChildItem
切换到
Get Item
,以及(b)在事件发生后过滤掉非目录,使您的命令正常工作,但这比上面的替代方法效率低:

Get-Item C:\thePath\* -Include p* | Where-Object PSIsContainer