A PowerShell oneliner that exponentially consumes all memory

Why would you want to write a script that intentionally consumes memory exponentially?
I find that for demo and test purposes this is very useful, especially when it comes to demonstrate Dynamic Memory in Hyper-V.

So I wrote the following oneliner:

1

do{$a+=Get-Process}until(!$a)

Or in short:

1

do{$a+=gps}until(!$a)

Now these oneliners will exponentially consume memory, but as you will notice it is very (!) slow…

You could use Get-ChildItem instead of Get-Process but it will give you errors for locations where it doesn’t have access to.
You could do something with the -ErrorAction parameter, but for the purpose of a demo that’s not required:

1

do{$a+=gcic:-rec}until(!$a)

But as I noticed some time ago in an ITCamp which I was attending there is a far more elegant and more effective way to accomplish this task!Jaap Brasser, also a member of DuPSUG, was attending the ITCamp as well and he came up with the following oneliner:

1

1..50|%{$x=1}{[array]$x+=$x}

This works great! Just try it and see your memory being consumed exponentially and much faster compared to the options I’ve previously mentioned in this post.