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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:49:17  来源:igfitidea点击:

Recursively list directories in powershell

windowspowershell

提问by Snowcrash

How do you recursively list directories in Powershell?

如何在 Powershell 中递归列出目录?

I tried dir /Sbut 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, diris an alias for the Get-ChildItemcmdlet.

在 PowerShell 中,dirGet-ChildItemcmdlet的别名。

Use it with the -Recurseparameter to list child items recursively:

将它与-Recurse参数一起使用以递归列出子项:

Get-ChildItem -Recurse

If you only want directories, and not files, use the -Directoryswitch:

如果您只需要目录而不是文件,请使用-Directory开关:

Get-ChildItem -Recurse -Directory

The -Directoryswitch is introduced for the file system provider in version 3.0.

-Directory开关是为 3.0 版中的文件系统提供程序引入的。

For PowerShell 2.0, filter on the PSIsContainerproperty:

对于 PowerShell 2.0,过滤PSIsContainer属性:

Get-ChildItem -Recurse |Where-Object {$_.PSIsContainer}

(PowerShell aliases support parameter resolution, so in all examples above, Get-ChildItemcan be replaced with dir)

(PowerShell 别名支持参数解析,因此在上面的所有示例中,Get-ChildItem都可以替换为dir