Skip to content

cruelandunusual/PowerShell_Get-Size

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Get-Size - a PowerShell cmdlet for human-readable file and folder size listings

Linux macOS Windows PowerShell

Description

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-childitem_output_screenshot

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:

get-size_output_screenshot

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.:

get-size_leftmost_column_screenshot

Usage

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:

get-size_get-help_output_screenshot

Notes

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.

Installation

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.

About

PowerShell Get-Size cmdlet for human-readable file and folder size listings, packaged as a module

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published