Show-TreeWithDates is a PowerShell function that generates a directory tree similar to the tree command, including the last modified date and time for each file and folder. It’s useful for audits, file inspections, or quick structure overviews on Windows, supporting recursive depth control, console output, and file export.
- Displays hierarchical directory structures with visual connectors
(│ ├── └──). - Includes last modification date and time for each item.
- Supports maximum recursion depth
(-MaxDepth). - Compatible with output redirection to files
(| Out-File "tree.txt"). - Silently ignores access errors
(-ErrorAction SilentlyContinue). - Sorts directories before files for cleaner visualization.
# Show current folder tree with modification dates
Show-TreeWithDates -Path "." -MaxDepth -1
# Show a specific folder tree
Show-TreeWithDates -Path "C:\Projects"
# Limit recursion depth
Show-TreeWithDates -Path "." -MaxDepth 2
# Save output to a text file
Show-TreeWithDates -Path "." | Out-File "tree.txt"
flowchart TD
A[Start] --> B[Receive parameters: Path, Prefix, MaxDepth, CurrentDepth, IsFirst]
B --> C{MaxDepth ≠ -1 and CurrentDepth ≥ MaxDepth?}
C -->|Yes| Z[End Function]
C -->|No| D{IsFirst = True?}
D -->|Yes| E[Get root item and show modification date]
D -->|No| F[Get child items using Get-ChildItem]
E --> F
F --> G[Sort: folders first, then files]
G --> H[Iterate over each item]
H --> I[Check if it is the last item at this level]
I --> J[Build visual connector ├── or └──]
J --> K[Display name and modification date]
K --> L{Is it a directory?}
L -->|Yes| M[Calculate new prefix and recursively call Show-TreeWithDates]
L -->|No| N[Continue to next item]
M --> N
N --> O[End loop]
O --> Z[End]