30

How do you recursively list directories in Powershell?

I tried dir /S but no luck:

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
Snowcrash
  • 80,579
  • 89
  • 266
  • 376

1 Answers1

67

In PowerShell, dir is an alias for the Get-ChildItem cmdlet.

Use it with the -Recurse parameter to list child items recursively:

Get-ChildItem -Recurse

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

Get-ChildItem -Recurse -Directory

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

For PowerShell 2.0, filter on the PSIsContainer property:

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

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

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206