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.

### Like this:

Like Loading...

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

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

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

would include the original number as well, which I think is implied in the example.

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))

Anything except PowerShell multipliers is absolutely allowed. All you need is the shortest answer!

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, $_))}

$size = 1234567890123456

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

PS C:> $size = 12345678901234568989

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

12056327051986884

11773756886706

11497809460

11228330

10965

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))}

Great idea using the compound assignment operator.

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

$s = 123456789

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

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)}

Fabulous!

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

would include the original number as well, which I think is implied in the example.

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

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