Friday, April 20, 2012

Force 100% CPU with PowerShell

For a course we are working on, we needed an application that would force 100% CPU utilization to do a troubleshooting activity. We had an old executable, that did this, but it didn't meet current compliance standards.

So, here is the PowerShell script (one line) that will max out your CPU:
while ($true) {$n++;$n=0}
I'm not sure that the $n=0 is required, but I'm concerned that it will go past the limit of the integer variable type if it runs for an extended period of time.

Also note that this maxes out only a single CPU core because PowerShell is a single process running on a single core. In our virtual machines for courses, this is fine because each virtual machine has only a single virtual CPU.

If you need to max out multiple CPU cores then you probably need to open multiple instances of PowerShell and run the script in each one.

2 comments:

  1. Hi, How can i simulate 100% CPU Usage in a 4 core processor?
    It seems this small piece of code generates max 25% of the CPU Usage. I need to use all cores and gain 100% CPU Usage.

    ReplyDelete
  2. As mentioned in the last line, you'll probably need to open multiple powershell prompts and run the script at each prompt. Then each instance of the script should max out a single core.

    ReplyDelete