Introduction to PowerShell’s Stop-Process
This page builds on the Get-Process command featured on the previous page. The idea is that once you have found, or ‘got’ a process, then you can zap it. Interestingly, Windows refers to this as ‘Killing’ the process, whereas PowerShell seeks consistency by always using ‘Stop’ and never ‘Kill’, ‘terminate’ or anything but ‘Stop’.
PowerShell Stop-Process Topics
- Our Mission
- Example 1: Failures
- Example 2: Eureka, Stop-Process Working
- Example 3: Simplify Stop-Process
- Example 4: PowerShell Kills the Process!
- Summary of PowerShell’s Stop-Process
Our Mission
Our mission is to kill, zap, or close a named process that you see in Task Manager. PowerShell either uses the verb ‘Stop’, or else the verb .Kill() for this job. I will show you three techniques to achieve the same goal of killing a process. If you examine each of the three methods and their associated learning points then you will gain extra insights into how PowerShell operates.
Preparation – Launch Task Manager
To help understand and to help troubleshooting, I thoroughly recommend the general principle of opening the GUI associated with the PowerShell object. Thus in the case of Get-Process and Stop-Process, I like to check the Image Names in Task Manager. The flashiest way to fire up the taskmgr is to press Ctrl +Shift +Esc. Next click on the Processes tab, if you click on ‘Image Name’, then you can sort the processes into alphabetical order.
Another advantage of viewing the processes in Task Manager is so that we can double-check the names. Notepad is easy, its process is Notepad! However, ‘Word for Windows’ is winword. One method of matching the names is to right-click the program in Task Manager’s Application tab, then select Go to Process.
Example 1: Failures – How NOT to Script Stop-Process
I don’t often employ the strategy of teaching by failures, but in the case of Stop-Process, I am going to make an exception. Please keep in mind that you always learn more when things go wrong!
Instructions:
Pre-requisite: Visit Microsoft’s site and download the correct version of PowerShell for your operating system.
- Launch PowerShell
- Copy the two lines of code below (into memory)
- Right-click on the PowerShell symbol
- Edit –> Paste
- Press enter to execute the code.
One more obvious pre-requisite, launch at least one instance of Notepad!
Problem: Even if you have started notepad, the following script will not stop the notepad process.
Clear-Host
Get-Process notepad Stop-Process
Learning Points
Note 1: I’ll be darned – all I get is errors, and the notepad is still running. Let us quickly move on to Example 2.
Guy Recommends: Network Performance Monitor (FREE TRIAL)
SolarWinds Network Performance Monitor (NPM) will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM on a 30-day free trial.
Example 2: Eureka, How to Get Stop-Process Working
All I needed was to pipe ‘Get-Process notepad’ into Stop-Process The result is its output becomes the input for Stop-Process. Here is the working example:
# PowerShell Kill Process
Clear-Host
Get-Process notepad | Stop-Process
Learning Points
Note 2: It’s all down to the (|) pipe.
Note 3: The knack is to employ Get-Process to get a grip on the named process object, then we use Stop-Process to remove it from the list of running programs.
See how Task Manager sets process priority in Windows 8 »
Example 3: Simplify Stop-Process
Typical Microsoft, there are always at least two way of achieving the same goal. Example 3 provides the simplest and most descriptive method of closing all notepad.exe programs.
# PowerShell 'Stops' Windows Process
Clear-Host
Stop-Process -name notepad
Learning Points
Note 4: Strictly speaking, the parameter is -processName
Note 5: The Stop-Process is versatile. If you execute a command with this Verb-Noun combination, then you need either the -name parameter, or the -id parameter. I prefer the -name construction; firstly it kills all instances of notepad, secondly, you don’t need to research the id number corresponding to a particular instance of the notepad process. Here is good example of the simplest programming construction also being the best.
Guy Recommends: SolarWinds Engineer’s Toolset (FREE TRIAL)
This Engineer’s Toolset provides a comprehensive console of 50 utilities for troubleshooting computer problems. Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.
There are so many good gadgets; it’s like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools. Try the SolarWinds Engineer’s Toolset on a 14-day free trial now!
Example 4: PowerShell Kills the Process!
Reverting to Get-Process, if you investigate the properties and especially the methods, you will see listed:
Kill Method System.void.kill().
We can put this knowledge to use by creating a variable to hold the notepad process object, then applying the .kill() method.
# PowerShell Kill Process
Clear-Host
$process = Get-Process notepad
$process.Kill()
Learning Points
Note 6: When researching PowerShell commands remember Get-Member or (gm). This is how I discovered .kill() method.
Get-Process | Get-Member.
Note 7: The first time I tried the .Kill method, it failed to work, all that I got was a definition of .kill. The simple reason was that I forgot the brackets .Kill(). Learn from my omission and remember to append those parenthesis.
Guy Recommends: SolarWinds Network Device Monitor (FREE TOOL)
Thus utility makes it easy to check the health of a router or firewall. Check the real-time performance, and availability statistics, for any device on your network. Get started with an extensive collection of “out-of-the-box” monitors for popular network devices. Give Network Monitor a whirl – it’s free. Download your free Network Device Monitor.
If you need more comprehensive network analysis software: Download a free trial of NPM (Network Performance Monitor)
Stop-Process Alias Kill
With Microsoft, there are always at least three ways of doing everything, what seems like redundancy when you are an expert, seems like perspective when you are a beginner. One obvious example is that you can abbreviate Format-Table to ft. As you increase your range of PowerShell commands, keep an eye out for another PowerShell Alias, for example gci (Get-Childitem).
PowerShell’s Process Family
To research any PowerShell keyword try Get-Command followed by -Noun keyword, or if it’s a doing word, -verb keyword. In the present example we want to discover other members of the process family.
# Research PowerShell's Process Family of Cmdlets
Clear-Host
Get-Command -Noun process
Note 8: PowerShell v 2.0 has a Start-Process command, I am not sure if this was present in PowerShell v 1.0.
See more tasks for PowerShell »
Summary of PowerShell’s Stop-Process Cmdlet
As you try each PowerShell command, get into the rhythm of Verb-Noun pairs such as Stop-Process. In this example, look out for PowerShell techniques such as pipeline and Get-Member. One real-life task is to create a script for killing processes, or as PowerShell says – Stop-Process.
See more PowerShell examples of process and service
• PowerShell Home • Get-Process • Stop-Process • PowerShell Start-Process • Set-Service
• Get-Service • Start-Service • Stop-Service • Restart-Service • Free WMI Monitor
• PowerShell Start-Sleep • Get-WmiObject win32_service • Windows PowerShell
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.