PowerShell Scripting Basics: Kill Process, Stop-Process Cmdlet

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

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.

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.

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.

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!

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.

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.

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.

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.