A high-performance PowerShell script that recursively scans directories and calculates folder sizes, similar to TreeSize. Uses parallel processing with runspaces for efficient scanning of large disk drives.
- Fast parallel directory scanning using PowerShell runspaces
- Thread-safe CSV output with real-time results
- Detailed logging of access errors and skipped items
- Configurable depth limits and exclusion patterns
- Handles junction points and symbolic links
- Outputs sizes in Bytes, MB, and GB
- Progress tracking during scan operations
- PowerShell 7.0 or higher
- Windows 10/11 (tested on PowerShell 7.5)
- .NET Framework (included with PowerShell)
- OutputCsv (Mandatory): Path to the output CSV file where results will be written.
- Path: Directory path to scan. Default: Current directory (
.) - MaxDepth: Maximum recursion depth. Default:
0(unlimited) - Exclude: Array of wildcard patterns to exclude from scanning. Default: Empty array
- RecurseLinkBehavior: How to handle symbolic links and junction points. Options:
Skip(default) orFollow - Parallelism: Number of parallel worker threads. Default:
ProcessorCount * 2 - VerboseLog: Path to the verbose log file. Default:
C:\Temp\TreeSizeVerbose.log
Scan the current directory and output to a CSV file:
.\get-treesize.ps1 -OutputCsv ".\results.csv"Scan the C: drive with 8 parallel workers:
.\get-treesize.ps1 -Path 'C:\' -Parallelism 8 -OutputCsv 'C:\Reports\c_drive_sizes.csv'Scan a directory excluding certain folders:
.\get-treesize.ps1 -Path 'D:\Projects' `
-Exclude @('*\node_modules\*', '*\.git\*', '*\bin\*') `
-OutputCsv 'D:\project_sizes.csv'Scan only 3 levels deep:
.\get-treesize.ps1 -Path 'C:\Users' -MaxDepth 3 -OutputCsv 'C:\users_tree.csv'.\get-treesize.ps1 `
-Path 'C:\' `
-MaxDepth 0 `
-Exclude @('*\Windows\*', '*\$Recycle.Bin\*') `
-RecurseLinkBehavior 'Skip' `
-Parallelism 8 `
-OutputCsv 'C:\Reports\c_drive_sizes.csv' `
-VerboseLog 'C:\Reports\treesize.log'The CSV file contains the following columns:
- Path: Full path to the directory
- Depth: Recursion depth (0 = root level)
- SizeBytes: Total size in bytes
- SizeMB: Total size in megabytes (rounded to 2 decimals)
- SizeGB: Total size in gigabytes (rounded to 2 decimals)
- FileCount: Number of files in the directory (not including subdirectories)
- Timestamp: ISO 8601 timestamp of when the directory was scanned
Path,Depth,SizeBytes,SizeMB,SizeGB,FileCount,Timestamp
"C:\Users",0,1234567890,1177.37,1.15,1250,2024-01-15T10:30:45.1234567Z
"C:\Users\John",1,987654321,941.89,0.92,850,2024-01-15T10:30:46.2345678Z- Uses concurrent collections (
ConcurrentDictionary,ConcurrentQueue) for thread-safe operations - Implements reader-writer locks for safe file writing
- Default parallelism is set to
ProcessorCount * 2for optimal performance - Progress is displayed in real-time during scanning
- Access denied errors are logged but do not stop the scan
- Inaccessible files and directories are skipped
- All errors are written to the verbose log file with timestamps
- The script continues processing even if individual directories fail
The verbose log file contains:
- Timestamped entries for each operation
- Excluded directories
- Skipped reparse points (junction points/symbolic links)
- File enumeration errors
- Directory access errors
- The script creates output directories automatically if they don't exist
- Large drives may take considerable time to scan completely
- Use exclusion patterns to skip known large directories (e.g.,
node_modules,.git) - Junction points and symbolic links are skipped by default to prevent infinite loops
Version 2.2 - Tested on PowerShell 7.5 / Windows 10-11
This script is provided as-is for use in scanning and analyzing disk usage.