Skip to content

Instantly share code, notes, and snippets.

@arenagroove
Created July 2, 2025 02:15
Show Gist options
  • Select an option

  • Save arenagroove/a238ed0abe94e9bdb2f045aa3272bbc8 to your computer and use it in GitHub Desktop.

Select an option

Save arenagroove/a238ed0abe94e9bdb2f045aa3272bbc8 to your computer and use it in GitHub Desktop.
PowerShell script to export a UTF-8 tree view of the current folder structure. Saves output as project-folder-structure.txt.
# Export-FolderTree.ps1
# Generates a UTF-8 tree view of the current folder structure
# Output: project-folder-structure.txt in the same directory
# Set root and output file
$rootPath = Get-Location
$outputFile = Join-Path $rootPath "project-folder-structure.txt"
# Force UTF-8 encoding
$encoding = [System.Text.Encoding]::UTF8
[System.IO.File]::WriteAllText($outputFile, "", $encoding)
# Recursive tree writing function
function Write-Tree {
param (
[string]$path,
[string]$prefix = "",
[bool]$isLast = $true
)
$item = Get-Item -LiteralPath $path
if ($item.FullName -eq $outputFile) { return }
$name = $item.Name
$connector = if ($isLast) { "└── " } else { "├── " }
$displayName = if ($item.PSIsContainer) { "$name/" } else { $name }
$line = "$prefix$connector$displayName"
[System.IO.File]::AppendAllText($outputFile, "$line`n", $encoding)
if ($item.PSIsContainer) {
$children = Get-ChildItem -LiteralPath $path |
Where-Object { $_.FullName -ne $outputFile } |
Sort-Object { -not $_.PSIsContainer }, Name
$count = $children.Count
for ($i = 0; $i -lt $count; $i++) {
$child = $children[$i]
$isLastChild = ($i -eq $count - 1)
$newPrefix = if ($isLast) { "$prefix " } else { "$prefix│ " }
Write-Tree -path $child.FullName -prefix $newPrefix -isLast $isLastChild
}
}
}
# Write root folder name
$rootName = Split-Path -Leaf $rootPath
[System.IO.File]::AppendAllText($outputFile, "$rootName/`n", $encoding)
# Begin from root
$rootItems = Get-ChildItem -LiteralPath $rootPath |
Where-Object { $_.FullName -ne $outputFile } |
Sort-Object { -not $_.PSIsContainer }, Name
$lastIndex = $rootItems.Count - 1
for ($i = 0; $i -lt $rootItems.Count; $i++) {
$child = $rootItems[$i]
$isLast = ($i -eq $lastIndex)
Write-Tree -path $child.FullName -prefix "" -isLast $isLast
}
Write-Output "✔ Tree written to: $outputFile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment