

In a cosmic perspective, most human concerns seem insignificant, even petty. Lost somewhere between immensity and eternity is our tiny planetary home. “The size and age of the Cosmos are beyond ordinary human understanding. To just get the total size (without enumerating any top level folders): function Get-FolderSize ' -f ($FolderSize.Sum / = $Path The main core of this script is the line (Get-ChildItem $folder\* -recurse) | measure-object -property length -sum which enumerates all the files in a folder and then passes them to Measure-Object to get the total size. $DirUseArray | Export-Csv -path C:\batch\DirUse.csv -NoTypeInformation $DirUseArray += New-Object PsObject -property = $_ # Convert from Bytes to GiB and round to 2 decimal places $size = (Get-ChildItem $folder\$_\* -recurse) | measure-object -property length -sum | Select-Object -expand sum # Read the size of this folder and all its sub folders. $firstLevel = Get-ChildItem $folder -directory

$DirUseArray = Get a list of first level sub-folders The script below is pure PowerShell and is kept as simple as possible to make it understandable - you may want to build on this and turn it into an advanced function. The old DirUse Resource kit utility can still be run in PowerShell to display disk usage for multiple subdirectories, but all it will do is display text it won’t provide any PowerShell objects we can manipulate. Each subfolder may have more subfolders and files.ġ.How-to: DIRUSE, a PowerShell script to list directory sizes Suppose we have a folder “d:\temp3” and this folder has multiple files and subfolders. $SubFolder: An optional parameter this conditional value will indicate whether to calculate each subfolder and file inside the original folder and will print separately.$FolderPath: A mandatory parameter it passes the folder or the drive name that we want to evaluate.$obj.Size_gb = $( ::Round( $subFolders.Sum / 1gb, 2)) $obj.Size_mb = $( ::Round( $subFolders.Sum / 1mb, 2)) $obj.Size_kb = $( ::Round( $subFolders.Sum / 1kb, 2)) # assign value to objects $obj.Folder = $i.FullName # empty object creation $obj = "" | select Folder, FolderCount, FileCount, Size_kb, Size_mb, Size_gb $result = Calculate parent folder grand total of all folder and file size, count $fileCount = ( Get-ChildItem $FolderPath -recurse | where-object ).Count # To get number of files and folder size # Version: 1.0 # Date: # function Get-FolderSize
