Powershell CPU Stress Test

I found myself needing to stress test a CPU for the purposes of running a laptop battery down to the point where it was safe to ship. I didn’t want to download any special tools to do this as I had sold the system to someone on eBay.

I figured PowerShell should be able to generate some multi-threaded CPU load, and here is what I successfully came up with:

# Set NumThreads to the number of cores your CPU has.
# If your CPU has HyperThreading, use the full thread count.
$NumThreads = 8

ForEach ($loop in 1..$NumThreads) {
    Start-Job -ScriptBlock {
        [float]$result = 1
        while ($true) {
            [float]$x = get-random -Minimum 1 -Maximum 999999999
            $result = $result * $x
        }
    }
}

I found that running in the ISE didn’t maximize the utilization of each job, so copying and pasting into a plain PowerShell window seems to be the way to go. Keep the window open, your child jobs die with the parent powershell.exe process.

Once running, you can check/manipulate the jobs if needed.

List all jobs:

get-job

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
1      Job1            BackgroundJob   Stopped       True            localhost            ...
3      Job3            BackgroundJob   Stopped       True            localhost            ...
5      Job5            BackgroundJob   Stopped       True            localhost            ...
7      Job7            BackgroundJob   Stopped       True            localhost            ...
9      Job9            BackgroundJob   Stopped       True            localhost            ...
11     Job11           BackgroundJob   Stopped       True            localhost            ...
13     Job13           BackgroundJob   Stopped       True            localhost            ...
15     Job15           BackgroundJob   Stopped       True            localhost            ...
17     Job17           BackgroundJob   Running       True            localhost            ...
19     Job19           BackgroundJob   Running       True            localhost            ...
21     Job21           BackgroundJob   Running       True            localhost            ...
23     Job23           BackgroundJob   Running       True            localhost            ...
25     Job25           BackgroundJob   Running       True            localhost            ...
27     Job27           BackgroundJob   Running       True            localhost            ...
29     Job29           BackgroundJob   Running       True            localhost            ...
31     Job31           BackgroundJob   Running       True            localhost            ...

List only running jobs:

get-job | where { $_.state -eq 'Running' }

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
17     Job17           BackgroundJob   Running       True            localhost            ...
19     Job19           BackgroundJob   Running       True            localhost            ...
21     Job21           BackgroundJob   Running       True            localhost            ...
23     Job23           BackgroundJob   Running       True            localhost            ...
25     Job25           BackgroundJob   Running       True            localhost            ...
27     Job27           BackgroundJob   Running       True            localhost            ...
29     Job29           BackgroundJob   Running       True            localhost            ...
31     Job31           BackgroundJob   Running       True            localhost            ...

Kill a single thread:

get-job Job17 | stop-job

Kill all threads:

get-job | stop-job