Skip to content

Commit 1498ca5

Browse files
authored
Merge pull request #21 from Miiraak/Add-get-tree-script
Adding Get-Tree.ps1 & Get-Tree.md
2 parents b47aebd + 8af3783 commit 1498ca5

2 files changed

Lines changed: 173 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Get-Tree.ps1
2+
3+
## Overview
4+
**Get-Tree.ps1** is a PowerShell script for Windows 10/11 that generates a recursive, ASCII directory tree of files and folders for a specified path.
5+
6+
## Features
7+
- **Tree generation:** Creates an ASCII tree structure of a given directory, showing nested folders and files.
8+
- **Recursive listing:** Explores all subdirectories and displays their contents in a hierarchical view.
9+
- **Easy to use:** Specify any folder to instantly visualize its structure.
10+
11+
## Requirements
12+
- **Windows 10/11**
13+
- **PowerShell 5.1+**
14+
- **A folder** (as input parameter)
15+
16+
## Parameters
17+
| Name | Type | Mandatory | Description |
18+
|--------|--------|-----------|---------------------------------------------|
19+
| Folder | string | Yes | The path of the folder to display as a tree |
20+
21+
## Usage
22+
### Basic Example
23+
```powershell
24+
.\Get-Tree.ps1 ./MyFolder
25+
.\Get-Tree.ps1 C:\Users\Miiraak\Desktop\
26+
```
27+
28+
## Script Workflow
29+
1. **Initialize:** Displays the root folder name.
30+
2. **Recursively gathers items:** Lists all files and folders starting from the provided path.
31+
3. **Formats output:** Prints each item with ASCII branches, clearly showing the folder structure.
32+
33+
## Output
34+
- **Console:**
35+
Displays the recursive tree structure, e.g.:
36+
```
37+
MyFolder
38+
├── file1.txt
39+
├── SubFolder
40+
│ ├── anotherfile.docx
41+
│ └── image.png
42+
└── README.md
43+
```
44+
45+
## Help & Documentation
46+
Get help in PowerShell:
47+
48+
```powershell
49+
Get-Help .\Get-Tree.ps1
50+
```
51+
or
52+
```powershell
53+
man .\Get-Tree.ps1
54+
```
55+
56+
## Troubleshooting
57+
- Run PowerShell as administrator for full access to all directories.
58+
- Some folders may require elevated permissions to access their contents.
59+
- If nothing is displayed, ensure the path is correct and the folder exists.
60+
61+
## License
62+
```
63+
MIT License
64+
65+
Copyright (c) 2025 Miiraak
66+
67+
Permission is hereby granted, free of charge, to any person obtaining a copy
68+
of this software and associated documentation files (the "Software"), to deal
69+
in the Software without restriction, including without limitation the rights
70+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
71+
copies of the Software, and to permit persons to whom the Software is
72+
furnished to do so, subject to the following conditions:
73+
74+
The above copyright notice and this permission notice shall be included in all
75+
copies or substantial portions of the Software.
76+
77+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
78+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
79+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
80+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
81+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
82+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
83+
SOFTWARE.
84+
```
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
###################################################################################################
2+
# #
3+
# ███▄ ▄███▓ ██▓ ██▓ ██▀███ ▄▄▄ ▄▄▄ ██ ▄█▀ #
4+
# ▓██▒▀█▀ ██▒▓██▒▓██▒▓██ ▒ ██▒▒████▄ ▒████▄ ██▄█▒ #
5+
# ▓██ ▓██░▒██▒▒██▒▓██ ░▄█ ▒▒██ ▀█▄ ▒██ ▀█▄ ▓███▄░ #
6+
# ▒██ ▒██ ░██░░██░▒██▀▀█▄ ░██▄▄▄▄██░██▄▄▄▄██ ▓██ █▄ #
7+
# ▒██▒ ░██▒░██░░██░░██▓ ▒██▒ ▓█ ▓██▒▓█ ▓██▒▒██▒ █▄ #
8+
# ░ ▒░ ░ ░░▓ ░▓ ░ ▒▓ ░▒▓░ ▒▒ ▓▒█░▒▒ ▓▒█░▒ ▒▒ ▓▒ #
9+
# ░ ░ ░ ▒ ░ ▒ ░ ░▒ ░ ▒░ ▒ ▒▒ ░ ▒ ▒▒ ░░ ░▒ ▒░ #
10+
# ░ ░ ▒ ░ ▒ ░ ░░ ░ ░ ▒ ░ ▒ ░ ░░ ░ #
11+
# ░ ░ ░ ░ ░ ░ ░ ░░ ░ #
12+
# #
13+
# Title : Get-Tree.ps1 #
14+
# Link : https://github.com/Miiraak/Scripts/tree/master/PowerShell/Discovery/Local/ #
15+
# Version : 1.1 #
16+
# Category : Discovery / Local #
17+
# Target : Windows 10/11 #
18+
# Description : Display a recursive ASCII tree of files and folders for a given path. #
19+
# #
20+
###################################################################################################
21+
22+
<#
23+
.SYNOPSIS
24+
Displays a visual ASCII tree of the directory structure for a specified folder.
25+
26+
.DESCRIPTION
27+
Recursively audits and prints all files and subdirectories within the given folder,
28+
presenting them in a clear, indented ASCII tree format. Useful for quickly inspecting
29+
the layout and contents of any directory.
30+
31+
.PARAMETER Folder
32+
The root folder path whose tree structure will be displayed.
33+
34+
.EXAMPLE
35+
.\Get-Tree.ps1 "C:\MyFolder"
36+
Shows the complete tree structure of the folder "C:\MyFolder" and all its subfolders.
37+
#>
38+
39+
# ---------------[Parameters]--------------- #
40+
param (
41+
[Parameter(Mandatory = $true)]
42+
[string]$Folder
43+
)
44+
45+
# ---------------[Functions]--------------- #
46+
function Show-Tree {
47+
param (
48+
[string]$Path,
49+
[string]$Prefix = ""
50+
)
51+
$items = Get-ChildItem -Path $Path
52+
$count = $items.Count
53+
for ($i = 0; $i -lt $count; $i++) {
54+
$item = $items[$i]
55+
if ($i -eq $count - 1) {
56+
$connector = "└── "
57+
} else {
58+
$connector = "├── "
59+
}
60+
Write-Output "$Prefix$connector$item"
61+
if ($item.PSIsContainer) {
62+
if ($i -eq $count - 1) {
63+
$newPrefix = $Prefix + " "
64+
} else {
65+
$newPrefix = $Prefix + ""
66+
}
67+
Show-Tree -Path $item.FullName -Prefix $newPrefix
68+
}
69+
}
70+
}
71+
72+
function Show-Summary {
73+
param (
74+
[string]$Path
75+
)
76+
$totalFiles = (Get-ChildItem -Path $Path -Recurse -File).Count
77+
$totalDirs = (Get-ChildItem -Path $Path -Recurse -Directory).Count
78+
$totalSize = (Get-ChildItem -Path $Path -Recurse -File | Measure-Object -Property Length -Sum).Sum
79+
Write-Output ""
80+
Write-Output "Total : $totalDirs dir, $totalFiles files, $totalSize bytes"
81+
}
82+
83+
# ------------[Execution]------------ #
84+
Write-Output (Split-Path $Folder -Leaf)
85+
Show-Tree -Path $Folder
86+
Show-Summary -Path $Folder
87+
88+
# ---------------[End]--------------- #
89+
exit 0

0 commit comments

Comments
 (0)