A Unix 'du' equivalent PowerShell script.txt - Notepad

A Unix 'du' equivalent PowerShell script

The below script will find the total size of a directory (includes all child objects). The script will ask you to manually enter the path of the directory. It will then return the total size of the directory in Megabytes (MB).
$startFolder = Read-Host "Enter directory"

$colItems = (Get-ChildItem $startFolder -recurse | Measure-Object -property length -sum)

"{0:N2}" -f ($colItems.sum / 1MB) + " MB"


The below script is similar to the above except that it returns the total size of each individual child folder.


$startFolder = Read-Host "Enter directory"

$colItems = (Get-ChildItem $startFolder | Measure-Object -property length -sum)

"$startFolder -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB"

$colItems = (Get-ChildItem $startFolder -recurse | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)

foreach ($i in $colItems)

{

$subFolderItems = (Get-ChildItem $i.FullName | Measure-Object -property length -sum)

$i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"

}


A useful modification to the above scripts is to replace 'Read-Host "Enter directory"' with 'pwd' eg:

$startFolder = pwd

This will allow you to cd to the desired directory using tab completion. You can then execute the script to find the size of your present working directory (pwd).

Back