CodeNinja
CodeNinja

Reputation: 3278

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 ?

Upvotes: 3

Views: 5219

Answers (2)

Esperento57
Esperento57

Reputation: 17462

Try -recurse option like this :

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

Upvotes: 4

Maybe you can use something like this:

 $Folders = Get-ChildItem C:\Folder\ -Directory -Name -Recurse

 foreach($Folder in $Folders)
 {
    $Folder = Split-Path -Path $Folder -Leaf

    Write-Host $Folder
 }

Upvotes: 1

Related Questions