0

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!

sloppyfrenzy
  • 253
  • 1
  • 5
  • 19
  • Possible duplicate of [Powershell display file size as KB, MB, or GB](https://stackoverflow.com/questions/24616806/powershell-display-file-size-as-kb-mb-or-gb) – Lance U. Matthews Aug 30 '17 at 19:37

4 Answers4

2

I've used this code many times:

# PowerShell Script to Display File Size
Function Format-DiskSize() {
[cmdletbinding()]
Param ([long]$Type)
If ($Type -ge 1TB) {[string]::Format("{0:0.00} TB", $Type / 1TB)}
ElseIf ($Type -ge 1GB) {[string]::Format("{0:0.00} GB", $Type / 1GB)}
ElseIf ($Type -ge 1MB) {[string]::Format("{0:0.00} MB", $Type / 1MB)}
ElseIf ($Type -ge 1KB) {[string]::Format("{0:0.00} KB", $Type / 1KB)}
ElseIf ($Type -gt 0) {[string]::Format("{0:0.00} Bytes", $Type)}
Else {""}
} # End of function
$BigNumber = "230993200055"
Format-DiskSize $BigNumber

Source: http://www.computerperformance.co.uk/powershell/powershell_function_format_disksize.htm

Ty Savercool
  • 1,132
  • 5
  • 10
1

As you used the -f operator, your output (here stored in $Pictures_Size_MB) is of the type System.String, hence the comparison operator doesn't work as you expected.

Try to do the math first, and then the formating. Like so:

$Pictures_Size = (Get-ChildItem $User\Pictures -recurse | Measure-Object -property length -sum).sum
if ($Pictures_Size -gt 1TB) { 
    # Output as double
    [System.Math]::Round(($Pictures_Size / 1TB), 2) 
    # Or output as string
    "{0:N2} TB" -f ($Pictures_Size / 1TB)
}
vrdse
  • 2,899
  • 10
  • 20
0

$Pictures_Size_MB contains string "5.05", which is greater than integer 1024, which is why the condition is met.

Honza Zíka
  • 495
  • 3
  • 12
  • How does it determine that 5.05 is greater than 1024? – sloppyfrenzy Aug 30 '17 at 18:36
  • I am not a powershell expert, but it seems like it is comparing string to a number, so it converts the number to string and then compares `"5.05"` to `"1024"`. If you compare it character by character, 5 is greater than 1, therefore 5.05 string is "bigger" than 1024 string. – Honza Zíka Aug 30 '17 at 18:39
  • Of course, because `$Pictures_Size_KB` contains `5171.2`, which is also greater than `1024` (because, again, 5 is bigger than 1). – Honza Zíka Aug 30 '17 at 18:46
0

You're using string formatters, thus storing your variable values as strings. Remove the unnecessary "{0:N2} -f" and instead use [Math]::Round()

Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63