Processes, Services and Jobs

What is running, starting and stopping programs, services with no window, and work in the background.

Updated

Programs

  1. What's Running: `Get-Process` and `Stop-Process` (Apprentice): Every running program is a process with a number, its Id: Get-Process lists them with their memory (WS) and processor time (CPU), Sort-Object puts the busiest first, CommandLine tells two PowerShells apart, and Stop-Process -Id stops one, after a rehearsal with -WhatIf.
  2. Starting Programs: `Start-Process` (Journeyman): Start-Process opens a file or folder in the app a double-click would use, starts a program with arguments in its own window or none, hands back its process with -PassThru, waits for it with -Wait or Wait-Process so you can read its ExitCode, and asks Windows for administrator rights with -Verb RunAs.

Services

  1. Services: `Get-Service` and `Restart-Service` (Journeyman): A service is a program Windows runs with no window, such as the Print Spooler: Get-Service shows each one's Status and, with Select-Object, its StartType, all without changing anything, while Restart-Service, Stop-Service, Start-Service and Set-Service change them and need an administrator window, after a rehearsal with -WhatIf.

Background Work

  1. Background Jobs and Parallel Work (Freelancer): Start-ThreadJob runs a script block in the background while you keep working, Get-Job shows its State, and Receive-Job hands over what it printed; ForEach-Object -Parallel does each item in its own runspace, -ThrottleLimit 4 at a time, which resizes 500 photos in a quarter of the time, with $using: to bring in a variable from outside.

Exercise: Stop the Runaway

  1. Exercise: Stop the Runaway (Journeyman): 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.