PowerShell

PowerShell: Calculate the total size of Files

In this post I will show how to use PowerShell to calculate the total size of all files. This will be a short post but maybe a very helpful one. Let’s dive in.

Open PowerShell ISE or VS Code. Copy the lines below into your PowerShell session and provide the path of your files.

$filespath = 'C:\Temp'
Get-ChildItem -Path $filespath -File -Recurse | 
Select-Object -ExpandProperty Length |
Measure-Object -Sum

Tip: If you want to search for specific files, or exclude some, you can use the include or exclude parameter. Here is an example.

Get-ChildItem -Path "C:\Users\patri\SynologyDrive\Videos\Udemy" -File -Exclude *.mp4 -Recurse | 
Select-Object -ExpandProperty Length |
Measure-Object -Sum

The value of the size is not optimal. That is why you can also display the value in GB.

Get-ChildItem -Path "C:\Users\patri\SynologyDrive\Videos\Udemy" -File -Recurse | 
Select-Object -ExpandProperty Length |
Measure-Object -Sum | Select-Object @{n='Size (GB)';e={$_.Sum/1GB}}

Now you are a master in calculating the size of files.

Categories: PowerShell

Tagged as: ,

3 replies »

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.