I have a form that shows the size of profile folders when a button is clicked. Here are a couple code variations I've tried for the Pictures folder...
$Pictures_Size = (Get-ChildItem $User\Pictures -recurse | Measure-Object -property length -sum)
$Pictures_Size_KB = "{0:N2}" -f ($Pictures_Size.sum / 1KB)
$Pictures_Size_MB = "{0:N2}" -f ($Pictures_Size.sum / 1MB)
$Pictures_Size_GB = "{0:N2}" -f ($Pictures_Size.sum / 1GB)
If ($Pictures_Size_KB -gt 1024) { $Pictures_Box.Text = "Pictures - $($Pictures_Size_MB) MB" }
If ($Pictures_Size_MB -gt 1024) { $Pictures_Box.Text = "Pictures - $($Pictures_Size_GB) GB" }
Else { $Pictures_Box.Text = "Pictures - $($Pictures_Size_KB) KB" }
and
$Pictures_Size = (Get-ChildItem $User\Pictures -recurse | Measure-Object -property length -sum)
$Pictures_Size_KB = "{0:N2}" -f ($Pictures_Size.sum / 1KB)
$Pictures_Size_MB = "{0:N2}" -f ($Pictures_Size.sum / 1MB)
$Pictures_Size_GB = "{0:N2}" -f ($Pictures_Size.sum / 1GB)
If ($Pictures_Size_MB -ge 1024) { $Pictures_Box.Text = "Pictures - $($Pictures_Size_GB) GB" }
If ($Pictures_Size_MB -lt 1024) { $Pictures_Box.Text = "Pictures - $($Pictures_Size_MB) MB" }
If ($Pictures_Size_KB -lt 1024) { $Pictures_Box.Text = "Pictures - $($Pictures_Size_KB) KB" }
The pictures folder I'm testing is 5 MB, but it shows up as 0.00 GB, and I can't figure out why. In the first code example, if I take out the If ($Pictures_Size_MB -gt 1024)
line it shows the size correctly at 5.05 MB. I'm not sure what's wrong because 5 is less than 1024 so it shouldn't be showing the GB number.
Please note this also needs to work in Windows 7!
Thanks!