title: PowerShell display files size as KB, MB, or GB
body: <p>I have a section of a PowerShell script that gets the file size of a specified directory.</p>
  
  <p>I am able to get the values for different units of measurement into variables, but I don't know a good way to display the appropriate one.</p>
  
  <pre><code>$DirSize = "{0:N2}" -f (($DirArray | Measure-Object -property length -sum).sum)
  $DirSizeKB = "{0:N2}" -f (($DirArray | Measure-Object -property length -sum).sum / 1KB)
  $DirSizeMB = "{0:N2}" -f (($DirArray | Measure-Object -property length -sum).sum / 1MB)
  $DirSizeGB = "{0:N2}" -f (($DirArray | Measure-Object -property length -sum).sum / 1GB)
  </code></pre>
  
  <p>If the number of bytes is at least 1&nbsp;KB I want the KB value displayed. If the number of KBs is at least 1&nbsp;MB I want MBs displayed and so on.</p>
  
  <p>Is there a good way to accomplish this?</p>