PowerShell – Get Size of Folder

Get size of folder in PowerShell

Using Get-ChildItem with Measure-Object

Use Get-ChildItem with the Measure-Object cmdlet to get the size of files in the specified directory.

Please note that above sum doesn’t include subfolder sizes. If you want to include subfolder as well, header over to this section.

The Measure-Object cmdlet was used to calculate the property values of objects. For example, we can use it to see the maximum, minimum and sum size of all files and folders in the given directory.

Here, the Sum field was used to display the size of all files in the directory, and count shows the number of files available. A switch parameter defined by -sum is used to add the size of each file shown in the Length field. Note: The sum of all files’ sizes is in bytes.

As you can observe, the total size of files in the C:\Users\DELL\Desktop\work directory is 208664 bytes given in the Sum field. Therefore, if you want to display the total file size of a given directory in KB, MB or GB, use the following commands.

In the above commands, gci is an alias of the Get-ChildItem cmdlet. To round the size to two decimals, check below:

We can observe above that the directory size in Kilobytes format with two decimal places is 203.77 KB and 0.00 GB and 0.20 MB, respectively. Therefore, we formatted the result using -f with Expression {0:N2} to get the result with two decimal places.

Get File Size of a Certain Type

Use Get-ChildItem with Measure-Object (whose alias is measure) cmdlet to get the size of a particular file type in the specified directory.

In the above script, gci is used (alias of Get-ChildItem cmdlet) to get the list of all the files in the specified directory that match the specified file type, let’s say.md.

We can customize this script to fit our needs. For example, we can change the path and file type to search a different directory or file type, or we can add additional actions to perform on each file.

Get Directory Size, Including Sub-folders

Use Get-ChildItem with the Measure-Object cmdlet to get the size of the directory, including sub-folders in the specified directory.

We can observe 560.64 KB is occupied by C:\Users\DELL\Desktop\work on my local drive (yours would be different). The -Recurse parameter was used to get the total size of files in the directory, including subdirectories, while the –force argument was used to consider the size of hidden and system files. Above, we used –ErrorAction SilentlyContinue.

Note: If an ErrorAction with the value SilentlyContinue is set, the program will continue to read a file or folder when we fail to read a file or folder for whatever reason, like lack of permissions or the file is locked by another process.

Using the PSFolderSize Module

Use the PSFolderSize module to get the file size in PowerShell.

NOTE: To use the PSFolderSize module. You can download it from PowerShell Gallery or install it by running the following command:

Once the module is installed, we can use the PSFolderSize cmdlet to get a detailed explanation of how to use PSFolderSize; run this command:

Note: To ensure the command runs, you must set the execution policy before moving forward. Follow this link for more information about setting ExecutionPolicy.

Use the Get-FolderSizeas follows to get the size of all files in the current directory:

It is also possible to get the size of any specific file from the folder. For example, I only want to get the size of the Downloads folder. See the following example to check how to do this:

You can run the following command to get the size of any specific folder.

By executing the command above, we can get the size of all the files in the Users folder in Megabytes and Giga Bytes.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *