Search code examples
powershellpowershell-2.0powershell-3.0powershell-4.0

PowerShell script to print all subfolders


I have a folder 'A' which as 2 subfolders 'A-1' & 'A-2' and 'A-1' has 2 more sub folders 'A-1-1' and 'A-1-2' under it.

I would like to display all the folder names using powershell script.

   $folders = Get-ChildItem C:\Folder\A -Directory
   foreach ($folder in $folders) {
    #For each folder in C:\banana, copy folder c:\copyme and its content to c:\banana\$folder
    #Copy-Item -Path "C:\copyme" -Destination "C:\banana\$($folder.name)" -Recurse
    write-host $folder
   }

My below script only prints :

A
A-1
A-2

I would like to print :

A
A-1
A-2
A-1-1
A-1-2

How can I do that ?


Solution

  • Try -recurse option like this :

    Get-ChildItem C:\Folder\ -Directory -recurse