This PowerShell Core Get-Size
cmdlet shows a human-readable size value when listing files and folders, similar to Bash du -h
.
Get-ChildItem
shows file sizes in bytes, which can be difficult to understand at a glance, and in any case it only shows the Length
property for files, not folders, e.g.:
Get-Size
gets the size of both files and folders, in a more easily understandable format.
The text in the size column is color-coded for clarity, with a different color for sizes up to 1MB, sizes between 1MB and 1GB, and for sizes 1GB and above. Sizes are truncated to two decimal places, e.g:
Folder highlighting is preserved as per the user's terminal colour settings, however the item Type
is shown in the leftmost column in case the user's colour settings don't differentiate files and folders. This is particularly useful because the results are sorted by either Name or Size, so folders and files may be adjacent in the results list, e.g.:
Input can be items (aka symbols) in a comma separated list, e.g.:
Get-Size D:\Music, D:\Pictures, myfile.txt
Variables storing a list of items can also be used, e.g.:
$mylist = Get-ChildItem D:\Documents\
Get-Size $mylist
Combinations of the two are supported:
Get-Size $mylist, D:\Pictures
The modifiable options are -SortProperty
and -Descending
/ -Ascending
.
-Descending
and -Ascending
are simple switches.
-SortProperty
expects either Name
or Size
.
The options can be given in any order, e.g.
Get-Size -Descending -SortProperty Name
Get-Size -SortProperty Name -Descending
By default Get-Size
sorts its results by size in ascending order, so -Ascending
can be ommitted.
Leaving out filenames, variables or wildcards will default to showing the content of the current directory, the same way Get-ChildItem
works, so the following are all equivalent:
Get-Size
Get-Size -Ascending
Get-Size -Ascending -SortProperty Size
Get-Size -Ascending -SortProperty Size *
Get-Size
is fully documented; type Get-Help Get-Size
for documentation on using the cmdlet, including examples:
Large folders with deep nested structures and many files may take some time to calculate. Write-Progress
has been implemented as a convenience to the user.
Get-Size
requires PowerShell Core, minimum version 7.0.
To install, clone this repo and copy the SizeModule folder to the Modules folder for your user, typically:
- Linux / macOS
- ~/.local/share/powershell/Modules
- Windows
- %USERPROFILE%\Documents\PowerShell\Modules
(Entering $env:PSModulePath
at the command prompt will reveal the location on your system.)
Tip
Set-Location ($env:PSModulePath).Split(';')[0]
# cd to the module location on Windows
Set-Location ($env:PSModulePath).Split(':')[0]
# macOS / Linux version
Then at the prompt enter:
Import-Module SizeModule
Once imported the module will be available for all future sessions.