# File Length Needing to be displayed as either MB or GB in code

**URL:** <https://forums.powershell.org/t/file-length-needing-to-be-displayed-as-either-mb-or-gb-in-code/15060>\
**Category:** PowerShell Help\
**Created:** [October 7, 2020, 12:03pm UTC](https://forums.powershell.org/t/file-length-needing-to-be-displayed-as-either-mb-or-gb-in-code/15060 "2020-10-07T12:03:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nal2us2](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nal2us2](https://forums.powershell.org/u/nal2us2)\
**Post date:** [October 7, 2020, 12:03pm UTC](https://forums.powershell.org/t/file-length-needing-to-be-displayed-as-either-mb-or-gb-in-code/15060/1 "2020-10-07T12:03:58Z")

</div>

probably a real easy one here to answer:.

&nbsp;

I am displaying a file size in a table that is very large.

&nbsp;

I need the code for how to display that same number in the Length column in either MB or GB.

&nbsp;

Please advise.

&nbsp;

Thank you

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [October 7, 2020, 12:22pm UTC](https://forums.powershell.org/t/file-length-needing-to-be-displayed-as-either-mb-or-gb-in-code/15060/2 "2020-10-07T12:22:07Z")

</div>

Typically we help you with code that you’ve written that you’re having issues with. However you are in luck as I recently answered a [question on Stackoverflow](https://stackoverflow.com/questions/63965384/how-to-convert-the-output-value-stored-in-variable-which-is-in-bytes-into-kb-mb) where the user needed to do the same thing with very large numbers.

```
Function Get-FriendlySize {
    Param([bigint]$bytes)
    switch($bytes){
        {$_ -gt 1PB}{"{0:N2} PB" -f ($_ / 1PB);break}
        {$_ -gt 1TB}{"{0:N2} TB" -f ($_ / 1TB);break}
        {$_ -gt 1GB}{"{0:N2} GB" -f ($_ / 1GB);break}
        {$_ -gt 1MB}{"{0:N2} MB" -f ($_ / 1MB);break}
        {$_ -gt 1KB}{"{0:N2} KB" -f ($_ / 1KB);break}
        default {"{0:N2} Bytes" -f $_}
    }
}
```

Call the function and pass in your number and it will return the formatted size. You may need to adjust this if you really only want MB or GB.

```
Get-FriendlySize 73068233
69.00 MB

Get-FriendlySize 5643986866
5.00 GB

Get-FriendlySize 91296907994238
83.00 TB
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/file-length-needing-to-be-displayed-as-either-mb-or-gb-in-code/15060/3 "2024-05-16T20:28:45Z")

</div>

Closed on May 16, 2024, 8:28pm UTC


