# Exercise: Stop the Runaway

Find three practice programs left running with no window, stop the runaway by its Id after a -WhatIf rehearsal, then write two tools, stop-runaway.ps1, which stops processes by a word in their command line and supports -WhatIf, and start-export.ps1, which starts a long export with no window and saves its Id, and let a Pester check tell you what's done.

Journeyman · PowerShell · Operating System · Updated November 10, 2026

It's the night before a deadline, and your laptop's fan is roaring. Three programs were left running with no window, so nothing on the taskbar gives them away: one is stuck in a loop, one beeps into a log every second, and one waits for a server that never answers. Meanwhile, the video export you actually need has to keep going in the background. You'll find the three, stop each one without touching anything else, and then write two small tools so you never have to do it by hand again: `stop-runaway.ps1`, which stops programs by a word in their command line and rehearses with `-WhatIf`, and `start-export.ps1`, which starts the export with no window and keeps its Id. A Pester check tells you what's done.

## Get the exercise code

Open this lesson's GitHub repository, [github.com/ask-codey/exercise-powershell-stop-the-runaway](https://github.com/ask-codey/exercise-powershell-stop-the-runaway), also linked from the **Exercise** box beside the lesson. Click **Code**, then **Download ZIP**, and unzip it into your `terminal-practice` folder. Or clone it with Git.

The exercise is for Windows, in PowerShell 7: Windows PowerShell 5.1 can't see a process's command line, which you'll need. The folder holds:

- `answers.txt`: five questions, each with a numbered line for your answer.
- `stop-runaway.ps1` and `start-export.ps1`: the two tools. Each starts with a comment that says exactly what it must do and print. These, and `answers.txt`, are the files you write.
- `start-the-mess.ps1`: starts the three misbehaving programs in `mess\`, each as its own PowerShell with no window. Each one stops by itself after 30 minutes, in case you forget it.
- `clean-up.ps1`: stops whatever the exercise left running, if you get stuck.
- `export-video.ps1`: the pretend export, one line a second into `export.log` for ten minutes.
- `Check.Tests.ps1`: the check. You don't need to change it.

Every program here is a practice program from this folder. Stop only these, and leave the rest of your computer's processes alone.

## What to do

Open PowerShell 7 in the exercise folder, and start the mess:

```powershell
.\start-the-mess.ps1
```

1. **Find the runaway.** All three are processes named `pwsh`, like your own window, so look at their command lines, as in *What's Running: `Get-Process` and `Stop-Process`*: `Get-Process -Name pwsh | Where-Object CommandLine -like '*mess*' | Select-Object Id, CPU, CommandLine`. Ask twice, a few seconds apart, and watch whose CPU climbs. Answer questions 1 to 5 in `answers.txt` as you go, after each `)`, like the example: `0) Get-Process`.
2. **Stop the runaway by hand.** Rehearse with `Stop-Process -Id` and `-WhatIf`, then stop it for real, and ask for its Id again to prove it's gone.
3. **Write `stop-runaway.ps1 -Name`.** It finds the `pwsh` processes whose command line has the name in it, leaves out its own (`$PID`), and stops each one inside `$PSCmdlet.ShouldProcess()`, as in *`-WhatIf` in Your Own Tools*. Try it on the other two: `.\stop-runaway.ps1 -Name mess -WhatIf`, then without `-WhatIf`.
4. **Write `start-export.ps1`.** It starts `export-video.ps1` with `Start-Process`, with no window and in its own folder, saves the Id that `-PassThru` hands back in `export-id.txt`, and finishes at once, as in *Starting Programs: `Start-Process`*. Run it, close the window, and open a new one in the folder. `Get-Content .\export.log -Tail 1` shows the export still going. Stop it with your own tool: `.\stop-runaway.ps1 -Name export-video`.

## Check your work

The check uses Pester. If you haven't installed it yet, install it once:

```powershell
Install-PSResource Pester
```

Then, from the exercise folder:

```powershell
Invoke-Pester
```

At first all fourteen tests fail. That's expected: each answer and each job you finish turns its test green. The check tries your two scripts for real, each in a PowerShell of its own, on small practice processes it starts itself, and it stops only the ones it started. It can take a minute, because it waits for them. A failing test says what to look at, for example *the script also stopped a process whose command line doesn't have the name in it. Stop only the ones Where-Object kept*.

If PowerShell says the check can't be loaded because it is not digitally signed, Windows has marked the downloaded files as coming from the internet: from the exercise folder, run `Get-ChildItem -Recurse | Unblock-File` once, then try again.

## Hints

- **Never by name.** `Stop-Process -Name pwsh` would close every PowerShell window you have, this one included. Stop by Id.
- **Find, then filter.** `Get-Process -Name pwsh | Where-Object { $_.Id -ne $PID -and $_.CommandLine -like "*$Name*" }` keeps the matches and leaves out your own PowerShell. Double quotes let `$Name` in.
- **Your script's own command line.** When the check runs `pwsh -File stop-runaway.ps1 -Name runaway`, the word `runaway` is in the command line of the PowerShell running your script. Without the `$PID` filter, it stops itself.
- **Rehearse inside the if.** Put `Stop-Process` and the *Stopped process* line both inside `if ($PSCmdlet.ShouldProcess(...)) { }`, so `-WhatIf` prints a *What if:* line and nothing else.
- **Don't wait.** `-WindowStyle Hidden -PassThru` without `-Wait` starts the export and hands it back at once. `-WorkingDirectory $PSScriptRoot` lets `.\export-video.ps1` be found from any folder.
- **Stuck?** `.\clean-up.ps1` stops everything the exercise started, and `.\start-the-mess.ps1` starts a fresh mess.

## When you're done

Every test is green, and Pester's last line says:

```text
Tests Passed: 14, Failed: 0, Skipped: 0, Inconclusive: 0, NotRun: 0
```

Now open Task Manager (**Ctrl+Shift+Esc**), start a fresh mess, and find the runaway on its *Details* page by the Id `Get-Process` gave you. Which is quicker for you, and which could you put in a script?

## Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.

1. On GitHub, click **+**, then **New repository**. Name it (for example `exercise-powershell-stop-the-runaway`), choose **Public**, and click **Create repository**. Leave out the README: your folder already has one.
2. In a terminal in your exercise folder (PowerShell works), send your work to your new repo. If you downloaded the ZIP:

   ```powershell
   git init -b main
   git add .
   git commit -m "My solution"
   git remote add origin https://github.com/your-name/exercise-powershell-stop-the-runaway.git
   git push -u origin main
   ```

   If you cloned the starter instead, point your copy at your own repo, then commit and push:

   ```powershell
   git remote set-url origin https://github.com/your-name/exercise-powershell-stop-the-runaway.git
   git add .
   git commit -m "My solution"
   git push -u origin main
   ```

3. Paste your repo's address, such as `https://github.com/your-name/exercise-powershell-stop-the-runaway`, into **Submit your exercise** on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

## So what

A runaway program is no longer a reason to restart the whole computer: you can find it, stop only it, and start the work that matters so it keeps going on its own.

## Key terms

- [Get-Process](https://www.ask-codey.com/glossary/get-process)
- [Stop-Process](https://www.ask-codey.com/glossary/stop-process)
- [Start-Process](https://www.ask-codey.com/glossary/start-process)
- [Process ID (PID)](https://www.ask-codey.com/glossary/process-id-pid)
- [-WhatIf and -Confirm](https://www.ask-codey.com/glossary/whatif-and-confirm)

## Related

- [What's Running: \`Get-Process\` and \`Stop-Process\`](https://www.ask-codey.com/lessons/powershell-processes)
- [Starting Programs: \`Start-Process\`](https://www.ask-codey.com/lessons/powershell-start-process)
- [Services: \`Get-Service\` and \`Restart-Service\`](https://www.ask-codey.com/lessons/powershell-services)
- [\`-WhatIf\` in Your Own Tools](https://www.ask-codey.com/lessons/powershell-supports-should-process)

Source: https://www.ask-codey.com/lessons/exercise-powershell-stop-the-runaway
