在powershell中递归列出目录

Sno*_*ash 7 windows powershell

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

我试过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
Run Code Online (Sandbox Code Playgroud)

Mat*_*sen 19

在PowerShell中,dirGet-ChildItemcmdlet 的别名.

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

Get-ChildItem -Recurse
Run Code Online (Sandbox Code Playgroud)

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

Get-ChildItem -Recurse -Directory
Run Code Online (Sandbox Code Playgroud)

-Directory在3.0版中为文件系统提供程序引入了此开关.

对于PowerShell 2.0,请对PSIsContainer属性进行过滤:

Get-ChildItem -Recurse |Where-Object {$_.PSIsContainer}
Run Code Online (Sandbox Code Playgroud)

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