I want to be able to see how big a folder is (all contents, including sub-folders and their contents). I can't find a powershell command to do that, but I don't want to have to open the windows explorer every time I want to know the size. Is there a simple way to accomplish this from within Powershell?
Asked
Active
Viewed 7.4k times
3 Answers
37
Pretty sure I got this from a Powershell tip of the day; can't remember for sure, but I've been using it a long time and it's been very useful.
"{0:N2}" -f ((Get-ChildItem -path C:\InsertPathHere -recurse | Measure-Object -property length -sum ).sum /1MB) + " MB"
Edit: To make it easier to use (so you don't have to remember and type this whole thing out each time) you could add it to your profile as a function, like so:
function Get-Size
{
param([string]$pth)
"{0:n2}" -f ((gci -path $pth -recurse | measure-object -property length -sum).sum /1mb) + " mb"
}
And then use it like any command:
Get-size C:\users\administrator
Darian Everett
- 746
10
It's on the Microsoft Technet site here
input:
Get-ChildItem C:\Scripts -recurse | Measure-Object -property length -sum
output:
Count : 58
Average :
Sum : 1244611
Maximum :
Minimum :
Property : length
50-3
- 3,959
-
1This can often be pretty far off though, as you need to recursively check if the folder has sub folders. Also, what is that measurement, bits? – Austin T French Aug 12 '13 at 21:48
-
Correct it's bits - also forgot to include the recurse switch which I have no edited in - also note I was mainly posting to provide the technet article on performing these search found via google (powershell folder size) so OP could get a full understanding of the commands used not just the answer – 50-3 Aug 12 '13 at 22:20
-
Thanks for the updates, its a better fit now I think for the Q&A format. – Austin T French Aug 13 '13 at 10:39
0
Others already posted great answers. Just in case anyone needs a more compact version you can use
ls path/ -r | measure length -s
which is a short form for @50-3's answer:
Get-ChildItem path/ -recurse | Measure-Object -property length -sum
To get output in MB use @Darian Everett's approach:
(ls path/ -r | measure length -s).sum/1mb
Elgirhath
- 111
To create your profile for the first time, do the following: ni $profile -type f -fo Then you can open and edit your profile. To find where it is located, just type $profile into the shell and the path will be displayed. Then any function you add, such as the one above, is available like it's built in. Thanks for the code formatting tip also – Darian Everett Aug 12 '13 at 21:34
{0:N2}do? – soandos Aug 12 '13 at 22:52-forceto list hidden files. And you can make the units part of the tokenized string, "{0:N2} MB", to avoid a string concatenation. – Anthony Mastrean Aug 19 '13 at 20:55+ " MB"from the end of that one liner to get numbers that are sortable in Excel (useful for multiple folders). – KERR Sep 13 '17 at 00:57