pGet in touch with us:  +44 (0) 1252 757181

Create a Fake high CPU workload with powershell

Friday, July 20, 2018
Dave Thomas

Sometimes it can be handy to generate a high CPU workload to test things out. Eg. to see if Application or environment manager type products  like Citrix WEM, Appsense AM are doing their job as expcted in controlling rogue processes. 

Calc.exe N! was always a favourite of mine, but in windows 10 this does not seem to generate a load anymore. 

I found the following powershell code to generate 100% CPU

Using the largest int32 positive integer, calculate the factorial to generate 100% CPU utilisation 
$result = 1; foreach ($number in 1..2147483647) {$result = $result * $number};

Depending on how fast the CPU is, this could finish, so a loop to run the command above 2 billion times:
foreach ($loopnumber in 1..2147483647) {$result=1;foreach ($number in 1..2147483647) {$result = $result * $number};$result}

If you want to see how long the command takes to run:
Measure-Command {$result = 1; foreach ($number in 1..2147483647) {$result = $result * $number}}

Using the command-line then provides the ability to run the command remotely. To use psexec to remotely execute powershell v1 factorial to generate 100% CPU:
psexec \\%computername% /s cmd /c "echo. | powershell $result = 1; foreach ($number in 1..2147483647) {$result = $result * $number}"