gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:55:58 28 4
gpt4 key购买 nike

我想获取所有以“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仅包含文件,不包括目录,请参见 this GitHub issue
您可以通过(a)从 Get-ChildItem切换到 Get-Item并(b)在事实结束后过滤掉非目录来使命令起作用,但这比上面的替代方法效率低:
Get-Item C:\thePath\* -Include p* | Where-Object PSIsContainer

关于powershell - 在PowerShell中获取以P开头的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63590402/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com