PowerShell脚本以打印所有子文件夹

时间:2018-06-22 20:22:33

标签: powershell powershell-v2.0 powershell-v3.0 powershell-v4.0

我有一个文件夹“ A”,其中有2个子文件夹“ A-1”和“ A-2”,而“ A-1”又有2个子文件夹“ A-1-1”和“ A-1-2” ”。

我想使用powershell脚本显示所有文件夹名称。

   $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
   }

我的以下脚本仅打印:

A
A-1
A-2

我想打印:

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

我该怎么做?

2 个答案:

答案 0 :(得分:2)

像这样尝试-recurse选项:

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

答案 1 :(得分:1)

也许您可以使用类似这样的东西:

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

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

    Write-Host $Folder
 }