Skip to content

Instantly share code, notes, and snippets.

@paalbra
Last active December 5, 2022 14:50
Show Gist options
  • Save paalbra/2ebf6764901fc4785edfb8c69072305d to your computer and use it in GitHub Desktop.
Save paalbra/2ebf6764901fc4785edfb8c69072305d to your computer and use it in GitHub Desktop.
DU/DiskUsage PowerShell wrapper around Sysinternals DU
<#
.SYNOPSIS
Command that provides disk usage for a given path.
.DESCRIPTION
Command that provides disk usage for a given path. Needs DU from Sysinternals.
.EXAMPLE
Get three levels of directories, sorted by level and size.
PS> .\du.ps1 -Path "C:" -AllLevels -Levels 3 | Sort-Object "Levels","Size" | Format-Table
.NOTES
Nothing will be output if the given path isn't as deep as the provided levels. Use AllLevels if you want output in this case.
.LINK
https://learn.microsoft.com/en-us/sysinternals/downloads/du
#>
[CmdletBinding()]
Param(
# Path to DU from Sysinternals
[Parameter(Mandatory=$false)] [string] $DUPath = "$env:TEMP\du64.exe",
# Output all levels
[Parameter(Mandatory=$false)] [switch] $AllLevels = $false,
# Subdirectories to output
[Parameter(Mandatory=$false)] [int] $Levels = 0,
# Path to check disk usage for
[Parameter(Mandatory=$true)] [string] $Path
)
Begin {
$Path = Resolve-Path "$Path"
$Path = $Path.TrimEnd("\")
$BaseLevels = ($Path.Split("\") | Measure-Object).Count
}
Process {
& "$DUPath" -l $Levels -c -nobanner "$Path" | ConvertFrom-Csv | Foreach-Object -ThrottleLimit 10 -Parallel {
$Levels = ($_.Path.Split("\") | Measure-Object).Count
if ($USING:AllLevels -or $Levels -eq ($USING:Levels + $USING:BaseLevels)) {
[PSCustomObject] @{
"Path" = $_.Path
"Levels" = ($_.Path.Split("\") | Measure-Object).Count
"FileCount" = [long] $_.CurrentFileCount
"TotalFileCount" = [long] $_.FileCount
"TotalDirectoryCount" = [long] $_.DirectoryCount - 1 # DirectoryCount includes current directory/itself.
"Size" = [long] $_.DirectorySizeOnDisk
"Size(MB)" = [math]::Round([long] $_.DirectorySizeOnDisk / 1MB, 2)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment