71

How do I list folders and files using the PowerShell tree command? Is it possible to color format the output for distinct files and folders?

Blackwood
  • 3,184
Tuomas Toivonen
  • 993
  • 2
  • 8
  • 10

4 Answers4

94

Simply use tree /F on any powershell instance to have the same behavior the normal tree on UNIX has.

The tree alone on Powershell shows only folders but not files in them.

Juansero29
  • 1,055
13
TREE Drive:\path /F

If command prompt or power shell are not recognizing 'tree', then:

Go to environment variables and change both the user variables and system variables "Path" to C:\Windows\System32 (Mine is C:\Windows\System32 ,Find yours using %systemroot%\System32)

Then open a new power shell and give the above command. EX: enter image description here

Sabito
  • 105
1

Is it possible to color format the output for distinct files and folders?

Yes, here's a PowerShell script to list folders and files, color files differently than folders, and adding file size as well as folder size:

function Get-FolderTreeWithSizes {
    param (
        [string]$Path = ".",
        [int]$Indent = 0
    )
$items = Get-ChildItem -Path $Path -Force
$folderSize = 0

foreach ($item in $items) {
    if ($item.PSIsContainer) {
        $subFolderSize = (Get-ChildItem -Path $item.FullName -Recurse -Force | Measure-Object -Property Length -Sum).Sum
        $subFolderSizeKB = "{0:N2} KB" -f ($subFolderSize / 1KB)
        Write-Host (" " * $Indent + "+-- " + $item.Name + " [Folder] (" + $subFolderSizeKB + ")") -ForegroundColor Green
        Get-FolderTreeWithSizes -Path $item.FullName -Indent ($Indent + 4)
    } else {
        $fileSizeKB = "{0:N2} KB" -f ($item.Length / 1KB)
        Write-Host (" " * $Indent + "+-- " + $item.Name + " (" + $fileSizeKB + ")") -ForegroundColor Yellow
        $folderSize += $item.Length
    }
}

if ($Indent -eq 0) {
    Write-Host ("Total Size of '$Path': {0:N2} KB" -f ($folderSize / 1KB)) -ForegroundColor Cyan
}

}

Get-FolderTreeWithSizes

Example output:

enter image description here

Franck Dernoncourt
  • 24,246
  • 64
  • 231
  • 400
0

I'd note the question asks about the Powershell tree command. The answers here are for tree.com, which is a separate executable residing at C:\Windows\System32\tree.com and hails from the cmd.exe days. It is unrelated to PowerShell.

There is no PowerShell tree command, though there is a tree alias in the Powershell Community Extensions, which is an alias of Show-Tree, and displays files via::

Show-Tree -ShowLeaf
c z
  • 139