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'.
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.
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 Firewall Browser
Here is an utility where you can review firewall settings such as
access control lists (ACL), or troubleshoot problems with network
address translation (NAT).
Other reasons to download this SolarWinds Firewall Browser include
managing requests to change your firewall settings, testing firewall
rules before you go live, and querying settings with the browser's
powerful search options.
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.
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: A Free Trial of the Network Performance Monitor
(NPM)
SolarWinds'
Network Performance Monitor
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 now.
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.
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.
# 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.
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.
If you like this page then please share it with your friends
See more PowerShell examples of process and service
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.
Windows Management Instrumentation (WMI) is
most useful for PowerShell scripting.
SolarWinds
have produced this
Free WMI Monitor to take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.