Measure-Object can also sum up property values. This is how you determine folder size. The following code calculates the complete folder size for your user profile (which can take some time depending on the number of files found). It simply sums up the property “Length” for all files:

$size = (Get-ChildItem -Path $home -Force -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum

"Folder Size: $sum Bytes"
'Folder Size: {0:n2} MB' -f ($size/1MB)

The output would look like this:

 
Folder Size: 172945767402 Bytes
Folder Size: 164.933,94 MB
 

You can explicitly control what you count by adding more parameters to Get-ChildItem. This code for example sums up only the sizes of PowerShell scripts found in your user profile by adding the –Filter parameter, and specifying the file extension:

$size = (Get-ChildItem -Path $home -Filter *.ps1 -Force -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum

"Folder Size: $size Bytes"
'Folder Size: {0:n2} MB' -f ($size/1MB)

Twitter This Tip! ReTweet this Tip!

Anonymous