17

Converting to size units (KB, MB,GB,TB, and PB) without using PowerShell multipliers

Hello everyone! After some time, here is the brand new brain teaser. We hope you will like the challenge. 🙂

In PowerShell, we can convert from bytes to KB, MB, GB, TB, and PB using the multipliers. For example,

$size = 123456789
$size / 1KB
$size / 1MB
$size / 1GB
$size / 1TB
$size / 1PB

Now, here is a task for you. You need to find a way to perform the above conversion without using any of the above PowerShell multipliers that is KB, MB, GB, TB, and PB. Here are some more rules:

1. If the result contains decimal point, round the number down. For example: 12.99 should become 12.
2. All versions of PowerShell are allowed, shortest way wins.

We teamed up with Packt Publishing to offer the winner of this contest a copy of Windows Server 2012 Automation with PowerShell Cookbook eBook.

Here is a quick overview of this book:

• Extend the capabilities of your Windows environment
• Improve the process reliability by using well defined PowerShell scripts
• Full of examples, scripts, and real-world best practices

This contest closes by Saturday, May 25. Wear your PowerShell wizard hat and post your answers in the Comments section. The winner will be announced next Monday.

Filed in: Brainteasers Tags: ,

17 Responses to "Converting to size units (KB, MB,GB,TB, and PB) without using PowerShell multipliers"

  1. VladimĂ­r Meier says:

    $i=1;1..5|%{[math]::truncate(123456789/[math]::pow(1024,$i));$i++}

    • VladimĂ­r Meier says:

      1..5|%{[math]::truncate(123456789/[math]::pow(1024,$_))}

      • ArtofThought says:

        0..5|%{[math]::truncate(123456789/[math]::pow(1024,$_))}
        would include the original number as well, which I think is implied in the example.

  2. James Birley says:

    Maybe I am missing something, but wouldn’t this do the trick? Or is using .NET not allowed?

    1
    2
    3
    4
    5
    6$size = 123456789

    [int]($size / [math]::pow(1024,1))
    [int]($size / [math]::pow(1024,2))

    [int]($size / [math]::pow(1024,3))

    [int]($size / [math]::pow(1024,4))

  3. Kevin Doblosky says:

    I added a few digits to $size, otherwise I get 0 for everything beyond GB:

    $size = 1234567890123456
    1..5 | % { [Math]::Floor($size/[Math]::Pow(1024, $_))}

  4. DVS says:

    $size = 1234567890123456
    1..5|%{“{0:N0}”-f($size/[math]::pow(1024,$_))}

  5. Vinith Menon says:

    PS C:> $size = 12345678901234568989

    PS C:> 1..5 | % { [math]::Round(($size/[Math]::Pow(1024,$_)),0)}

    12056327051986884

    11773756886706

    11497809460

    11228330

    10965

  6. Jaap Brasser says:

    My solution would be to bypass using [Math]::Pow in order to make it shorter. So I would go for this:

    $size = 1234567890123456

    1..5|%{[Math]::Floor(($size/=1024))}

  7. Rob Campbell says:

    Keep the compound operators, but trade [Math]::Floor for [int]-.5, so it always rounds down.

    $s = 123456789
    1..5|%{[int](($s/=1024)-.5)}

    • Jaap Brasser says:

      Cool, I think anyone would be hard pressed to make it shorter than this. Nice solution, the downside is that you will be unable to calculate actual petabytes if you wanted to. Then you would have to resort to this:
      $s = 12345678964561111
      1..5|%{[long](($s/=1024)-.5)}

    • DVS says:

      Fabulous!

  8. ArtofThought says:

    0..5|%{[math]::truncate(123456789/[math]::pow(1024,$_))}
    would include the original number as well, which I think is implied in the example.

  9. Max Kozlov says:

    I’m too late but i think this is the shortest

    1..5|%{1234567890 -shr $_*10}

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: