ちょとした調査やドキュメント作成にもよく活躍しています。アイコンはお好みで使い分けてください。
指定のフォルダを階層表示
function Show-Tree {
param(
[string]$Path,
[int]$Depth = 5,
[string]$FolderIcon = "📁",
[string]$FileIcon = "📄",
[bool]$ShowRoot = $true,
[int]$Level = 0
)
if ($Level -eq 0 -and $ShowRoot) {
Write-Output ("$FolderIcon " + (Split-Path $Path -Leaf))
}
if ($Level -ge $Depth) { return }
Get-ChildItem -Path $Path | ForEach-Object {
$indent = " " * ($Level)
if ($_.PSIsContainer) {
Write-Output ("$indent|-- $FolderIcon " + $_.Name)
Show-Tree -Path $_.FullName -Depth $Depth -Level ($Level + 1) -ShowRoot:$false -FolderIcon $FolderIcon -FileIcon $FileIcon
} else {
Write-Output ("$indent|-- $FileIcon " + $_.Name)
}
}
}
利用例
デフォルト アイコン表示
Show-Tree -Path "myenv" -Depth 2
[DIR] [FILE]で表現
Show-Tree -Path "myenv" -Depth 2 -FolderIcon "[DIR]" -FileIcon "[FILE]"
フォルダを/で表現
Show-Tree -Path "myenv" -Depth 2 -FolderIcon "/" -FileIcon ""
実行例
-- まずはデフォルト
--
test >> Show-Tree -Path "myenv" -Depth 2
📁 myenv
|-- 📁 Include
|-- 📁 Lib
|-- 📁 site-packages
|-- 📁 Scripts
|-- 📄 activate
|-- 📄 activate.bat
|-- 📄 Activate.ps1
|-- 📄 deactivate.bat
|-- 📄 pip.exe
|-- 📄 pip3.12.exe
|-- 📄 pip3.exe
|-- 📄 python.exe
|-- 📄 pythonw.exe
|-- 📄 pyvenv.cfg
-- [DIR] [FILE]で表現
test >> Show-Tree -Path "myenv" -Depth 2 -FolderIcon "[DIR]" -FileIcon "[FILE]"
[DIR] myenv
|-- [DIR] Include
|-- [DIR] Lib
|-- [DIR] site-packages
|-- [DIR] Scripts
|-- [FILE] activate
|-- [FILE] activate.bat
|-- [FILE] Activate.ps1
|-- [FILE] deactivate.bat
|-- [FILE] pip.exe
|-- [FILE] pip3.12.exe
|-- [FILE] pip3.exe
|-- [FILE] python.exe
|-- [FILE] pythonw.exe
|-- [FILE] pyvenv.cfg
-- シンプルに
test >> Show-Tree -Path "myenv" -Depth 2 -FolderIcon "/" -FileIcon ""
/ myenv
|-- / Include
|-- / Lib
|-- / site-packages
|-- / Scripts
|-- activate
|-- activate.bat
|-- Activate.ps1
|-- deactivate.bat
|-- pip.exe
|-- pip3.12.exe
|-- pip3.exe
|-- python.exe
|-- pythonw.exe
|-- pyvenv.cfg
test >>