Skip to content

santosh09142/tree-size-powershell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

Get-TreeSize PowerShell Script

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.

Features

  • 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

Requirements

  • PowerShell 7.0 or higher
  • Windows 10/11 (tested on PowerShell 7.5)
  • .NET Framework (included with PowerShell)

Parameters

Required Parameters

  • OutputCsv (Mandatory): Path to the output CSV file where results will be written.

Optional Parameters

  • 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) or Follow
  • Parallelism: Number of parallel worker threads. Default: ProcessorCount * 2
  • VerboseLog: Path to the verbose log file. Default: C:\Temp\TreeSizeVerbose.log

Usage Examples

Basic Usage

Scan the current directory and output to a CSV file:

.\get-treesize.ps1 -OutputCsv ".\results.csv"

Scan Entire Drive

Scan the C: drive with 8 parallel workers:

.\get-treesize.ps1 -Path 'C:\' -Parallelism 8 -OutputCsv 'C:\Reports\c_drive_sizes.csv'

Scan with Exclusions

Scan a directory excluding certain folders:

.\get-treesize.ps1 -Path 'D:\Projects' `
    -Exclude @('*\node_modules\*', '*\.git\*', '*\bin\*') `
    -OutputCsv 'D:\project_sizes.csv'

Limited Depth Scan

Scan only 3 levels deep:

.\get-treesize.ps1 -Path 'C:\Users' -MaxDepth 3 -OutputCsv 'C:\users_tree.csv'

Full Example with All Options

.\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'

Output Format

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

Example CSV Output

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

Performance

  • Uses concurrent collections (ConcurrentDictionary, ConcurrentQueue) for thread-safe operations
  • Implements reader-writer locks for safe file writing
  • Default parallelism is set to ProcessorCount * 2 for optimal performance
  • Progress is displayed in real-time during scanning

Error Handling

  • 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

Logging

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

Notes

  • 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

Version 2.2 - Tested on PowerShell 7.5 / Windows 10-11

License

This script is provided as-is for use in scanning and analyzing disk usage.

About

TreeSize powershell script

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors