The *list-dir-tree.ps1* Script =========================== This PowerShell script lists all files and folders in a neat directory tree (including icon and size). Parameters ---------- ```powershell /home/markus/Repos/PowerShell/scripts/list-dir-tree.ps1 [[-path] ] [] -path Specifies the path to the directory tree Required? false Position? 1 Default value "$PWD" Accept pipeline input? false Accept wildcard characters? false [] This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer, PipelineVariable, and OutVariable. ``` Example ------- ```powershell PS> ./list-dir-tree.ps1 C:\MyFolder β”œπŸ“‚Results β”‚ β”œπŸ“„sales.txt (442K) (2 folders, 1 file, 442K file size in total) ``` Notes ----- Author: Markus Fleschutz | License: CC0 Related Links ------------- https://github.com/fleschutz/PowerShell Script Content -------------- ```powershell <# .SYNOPSIS Lists a dir tree .DESCRIPTION This PowerShell script lists all files and folders in a neat directory tree (including icon and size). .PARAMETER path Specifies the path to the directory tree .EXAMPLE PS> ./list-dir-tree.ps1 C:\MyFolder β”œπŸ“‚Results β”‚ β”œπŸ“„sales.txt (442K) (2 folders, 1 file, 442K file size in total) .LINK https://github.com/fleschutz/PowerShell .NOTES Author: Markus Fleschutz | License: CC0 #> param([string]$path = "$PWD") function GetFileIcon([string]$suffix) { switch ($suffix) { ".csv" {return "πŸ“Š"} ".epub" {return "πŸ““"} ".exe" {return "βš™οΈ"} ".gif" {return "πŸ“Έ"} ".iso" {return "πŸ“€"} ".jpg" {return "πŸ“Έ"} ".mp3" {return "🎡"} ".mkv" {return "🎬"} ".png" {return "πŸ“Έ"} ".rar" {return "🎁"} ".tar" {return "🎁"} ".zip" {return "🎁"} default {return "πŸ“„"} } } function Bytes2String([int64]$bytes) { if ($bytes -lt 1000) { return "$bytes bytes" } $bytes /= 1000 if ($bytes -lt 1000) { return "$($bytes)K" } $bytes /= 1000 if ($bytes -lt 1000) { return "$($bytes)MB" } $bytes /= 1000 if ($bytes -lt 1000) { return "$($bytes)GB" } $bytes /= 1000 return "$($Bytes)TB" } function ListDir([string]$path, [int]$depth) { $depth++ $items = Get-ChildItem -path $path foreach($item in $items) { $filename = $item.Name for ($i = 1; $i -lt $depth; $i++) { Write-Host "β”‚ " -noNewline } if ($item.Mode -like "d*") { Write-Output "β”œπŸ“‚$Filename" ListDir "$path\$filename" $depth } else { $icon = GetFileIcon $item.Extension Write-Output "β”œ$($icon)$filename ($(Bytes2String $item.Length))" $global:files++ $global:bytes += $item.Length } } $global:folders++ } try { [int64]$global:folders = 0 [int64]$global:files = 0 [int64]$global:bytes = 0 ListDir $path 0 Write-Output " ($($global:folders) folders, $($global:files) files, $(Bytes2String $global:bytes) total)" exit 0 # success } catch { "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" exit 1 } ``` *(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:55)*