Table of Contents
Using Get-ChildItem
with Measure-Object
Use Get-ChildItem
with the Measure-Object
cmdlet to get the size of files in the specified directory.
1 2 3 |
Get-ChildItem C:\Users\DELL\Desktop\work | Measure-Object -Property Length -sum |
1 2 3 4 5 6 7 8 |
Count: 16 Average: Sum: 208664 Maximum: Minimum: Property: Length |
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.
1 2 3 4 5 |
(gci C:\Users\DELL\Desktop\work | measure Length -s).sum / 1Kb (gci C:\Users\DELL\Desktop\work | measure Length -s).sum / 1Gb (gci C:\Users\DELL\Desktop\work | measure Length -s).sum / 1Mb |
1 2 3 4 5 |
203.7734375 0.000194333493709564 0.198997497558594 |
In the above commands, gci
is an alias of the Get-ChildItem
cmdlet. To round the size to two decimals, check below:
1 2 3 4 5 |
"{0:N2} KB" -f ((gci C:\Users\DELL\Desktop\work | measure Length -s).sum / 1Kb) "{0:N2} GB" -f ((gci C:\Users\DELL\Desktop\work | measure Length -s).sum / 1Gb) "{0:N2} MB" -f ((gci C:\Users\DELL\Desktop\work | measure Length -s).sum / 1Mb) |
1 2 3 4 5 |
203.77 KB 0.00 GB 0.20 MB |
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.
1 2 3 |
(gci C:\Users\DELL\Desktop\work *.md | measure Length -s).sum / 1Mb |
1 2 3 |
0.074549674987793 |
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.
1 2 3 |
"{0:N2} KB" -f ((gci –force C:\Users\DELL\Desktop\work –Recurse -ErrorAction SilentlyContinue| measure Length -s).sum / 1Kb) |
1 2 3 |
560.64 KB |
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:
1 2 3 |
Install-Module PSFolderSize |
Once the module is installed, we can use the PSFolderSize
cmdlet to get a detailed explanation of how to use PSFolderSize
; run this command:
1 2 3 |
Get-Help Get-FolderSize -Detailed |
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-FolderSize
as follows to get the size of all files in the current directory:
1 2 3 |
Get-FolderSize |
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:
1 2 3 |
Get-FolderSize -Name Downloads |
1 2 3 4 5 |
FolderName SizeMB SizeGB FullPath ---------- ------ ------ -------- Downloads 495.82 0.48 C:\Users\DELL\Downloads |
You can run the following command to get the size of any specific folder.
1 2 3 |
Get-FolderSize -Path C:\Users |
1 2 3 4 5 6 7 8 9 10 |
FolderName SizeMB SizeGB FullPath ---------- ------ ------ -------- DELL 7197.79 7.03 C:\Users\DELL All Users 1988.72 1.94 C:\Users\All Users Default 1.51 0 C:\Users\Default Public 0.01 0 C:\Users\Public desktop 0 0 C:\Users\desktop.ini Default User C:\Users\Default User |
By executing the command above, we can get the size of all the files in the Users
folder in Megabytes and Giga Bytes.