PowerShell: Show size of a folder

PowerShell: Show size of a folder

When doing a project and watching the progress, e. g. when copying very many files or very large ones, you want to know the size of a folder.

On Linux, you can use df:

# du -sh /u01

And if you want to keep this information updated, you can use watch to repeat this command:

# watch -n 1 du -sh /u01

On Windows, we do not have commands like du or watch. But as PowerShell is a very powerful tool, we can use it for that:

[math]::Round((Get-ChildItem -Path c:\temp -Recurse | Measure-Object -Property Length -Sum).Sum / 1GB,2)

It will display something like that:

0,68

So the size is 0.68 GB (my output is displayed using german decimals).

If you want to repeat the command, you can use this script - it will repeat it every 10 seconds:

while($True){cls;[math]::Round((Get-ChildItem -Path c:\temp -Recurse | Measure-Object -Property Length -Sum).Sum / 1GB,2);sleep 10}

If you're a fan of PowerShell and prefer it over using shell scripts, you can use it also on Windows.

# pwsh
PowerShell 7.4.6
PS /tmp> while($True){cls;[math]::Round((Get-ChildItem -Path /tmp -Recurse | Measure-Object -Property Length -Sum).Sum / 1GB,2);sleep 10}
0,15
Here is how to install PowerShell on Windows, Linux and other platforms