Link to home
Start Free TrialLog in
Avatar of techdrive
techdriveFlag for United States of America

asked on

Converting data to KB, MB, GB

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" } }}
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of techdrive

ASKER

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
What exactly do you have in that text file of yours?

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