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
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, 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.ps1andstart-export.ps1: the two tools. Each starts with a comment that says exactly what it must do and print. These, andanswers.txt, are the files you write.start-the-mess.ps1: starts the three misbehaving programs inmess\, 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 intoexport.logfor 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:
.\start-the-mess.ps1
- 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-ProcessandStop-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 inanswers.txtas you go, after each), like the example:0) Get-Process. - Stop the runaway by hand. Rehearse with
Stop-Process -Idand-WhatIf, then stop it for real, and ask for its Id again to prove it's gone. - Write
stop-runaway.ps1 -Name. It finds thepwshprocesses whose command line has the name in it, leaves out its own ($PID), and stops each one inside$PSCmdlet.ShouldProcess(), as in-WhatIfin Your Own Tools. Try it on the other two:.\stop-runaway.ps1 -Name mess -WhatIf, then without-WhatIf. - Write
start-export.ps1. It startsexport-video.ps1withStart-Process, with no window and in its own folder, saves the Id that-PassThruhands back inexport-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 1shows 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:
Install-PSResource Pester
Then, from the exercise folder:
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 pwshwould 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$Namein. - Your script's own command line. When the check runs
pwsh -File stop-runaway.ps1 -Name runaway, the wordrunawayis in the command line of the PowerShell running your script. Without the$PIDfilter, it stops itself. - Rehearse inside the if. Put
Stop-Processand the Stopped process line both insideif ($PSCmdlet.ShouldProcess(...)) { }, so-WhatIfprints a What if: line and nothing else. - Don't wait.
-WindowStyle Hidden -PassThruwithout-Waitstarts the export and hands it back at once.-WorkingDirectory $PSScriptRootlets.\export-video.ps1be found from any folder. - Stuck?
.\clean-up.ps1stops everything the exercise started, and.\start-the-mess.ps1starts a fresh mess.
When you're done
Every test is green, and Pester's last line says:
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.
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.In a terminal in your exercise folder (PowerShell works), send your work to your new repo. If you downloaded the ZIP:
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 mainIf you cloned the starter instead, point your copy at your own repo, then commit and push:
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 mainPaste 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.