Converting data to KB, MB, GB

Posted on 2011-05-11
Last Modified: 2012-06-27
I have been trying to convert raw numbers in a file to KB, GB or MB. For ex

get-content c:\temp\numbers.txt | select -object Name, {$_.Somename.value.ToMB()}

the text file contain numbers of different sizes that were recorded from another sheet.

or I would really like to pipe it into something like this

  $_ | Select-Object Name, @{n='Count';e={ $Count }}, @{n='Size';e={
    If     ($Size -gt 1GB) { "$('{0:N2}' -f ($Size / 1Gb)) GB" }
    ElseIf ($Size -gt 1MB) { "$('{0:N2}' -f ($Size / 1Mb)) MB" }
    ElseIf ($Size -gt 1KB) { "$('{0:N2}' -f ($Size / 1Kb)) KB" }
    Else                   { "$Size b" } }}
Question by:techdrive
    LVL 70

    Accepted Solution

    It's not quite as complex as the two snippets you have.
    Get-Content c:\temp\numbers.txt | ForEach-Object {
      If ($_ -gt 1GB) { 
        "$('{0:N2}' -f ($_ / 1Gb)) GB" 
      } ElseIf ($_ -gt 1Mb) {
        "$('{0:N2}' -f ($_ / 1Mb)) MB"
      } ElseIf ($_ -gt 1Kb) {
        "$('{0:N2}' -f ($_ / 1Kb)) KB"
      } Else {
        "$_ b"

    Open in new window

    Is that what you had in mind?


    Author Comment

    The logic is nice however I am getting the following below when I run this

    Cannot compare "@{Name=439150512}" to "1073741824" because the objects are not the same type or the object "@{Name=439150512}" does not implement "IComparable".
    At C:\temp\convert.ps1:2 char:13
    +   If ($_ -gt <<<<  1GB) {
        + CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemException
        + FullyQualifiedErrorId : PSObjectCompareTo
    LVL 70

    Expert Comment

    by:Chris Dent
    What exactly do you have in that text file of yours?


    Author Comment

    Chris sorry the machine I was using is locked down. This worked perfectly thanks again.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    A commonly asked question on the site recently has been regarding how do I share the GAL between different forests? I want a list of Contacts representing all the users in a remote domain, How do i get them? It's a problem I was posed with a litt…
    Set OWA language and time zone in Exchange for individuals, all users or per database.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now