windows 在powershell中递归列出目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42440753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recursively list directories in powershell
提问by Snowcrash
How do you recursively list directories in Powershell?
如何在 Powershell 中递归列出目录?
I tried dir /S
but no luck:
我试过dir /S
但没有运气:
PS C:\Users\snowcrash> dir /S
dir : Cannot find path 'C:\S' because it does not exist.
At line:1 char:1
+ dir /S
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\S:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
回答by Mathias R. Jessen
In PowerShell, dir
is an alias for the Get-ChildItem
cmdlet.
在 PowerShell 中,dir
是Get-ChildItem
cmdlet的别名。
Use it with the -Recurse
parameter to list child items recursively:
将它与-Recurse
参数一起使用以递归列出子项:
Get-ChildItem -Recurse
If you only want directories, and not files, use the -Directory
switch:
如果您只需要目录而不是文件,请使用-Directory
开关:
Get-ChildItem -Recurse -Directory
The -Directory
switch is introduced for the file system provider in version 3.0.
该-Directory
开关是为 3.0 版中的文件系统提供程序引入的。
For PowerShell 2.0, filter on the PSIsContainer
property:
对于 PowerShell 2.0,过滤PSIsContainer
属性:
Get-ChildItem -Recurse |Where-Object {$_.PSIsContainer}
(PowerShell aliases support parameter resolution, so in all examples above, Get-ChildItem
can be replaced with dir
)
(PowerShell 别名支持参数解析,因此在上面的所有示例中,Get-ChildItem
都可以替换为dir
)