Windows PowerShell


PowerShell Scripting with stop-Process

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 stop 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.

  ˚

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:

# clear-Host
 get-Process notepad | stop-Process

Learning Points

Note 1:  It's all down to the (|) pipe.

Note 2:  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.

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.

# clear-Host
 stop-Process -name notepad

Learning Points

Note 1: Strictly speaking, the parameter is -processName

Note 1:  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.

Example 4: Kill 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.

$process = get-Process notepad
$process.Kill()

Learning Points

Note 1:  When researching PowerShell commands remember get-Member or (gm).  This is how I discovered .kill().
get-Process | get-Member.

Note 2:  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 LANSurveyorSolarwinds LANSurveyor

LANSurveyor will produce a neat diagram of your network topology.  But that's just the start; LANSurveyor can create an inventory of the hardware and software of your machines and network devices.  Other neat features include dynamic update for when you add new devices to your network.  I also love the ability to export the diagrams to Microsoft Visio.

Finally, Guy bets that if you take a free trial of LANSurveyor then you will find a device on your network that you had forgotten about, or someone else installed without you realizing!

Download a Free Trial of LANSurveyor

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.

get-Command -noun process

Note1:  Surprise, surprise although there is get-Process and a stop-Process there is no start-Process command.

Summary of PowerShell's stop-Process

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  • get-Service  • start-Service  • gwmi win32_service

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2009 Computer Performance LTD All rights reserved

Please report a broken link, or an error.