PowerShell Scripting with stop-ProcessIntroduction to PowerShell's Stop ProcessThis 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 MissionOur 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 ManagerTo 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: FailuresI don't often employ the strategy of teaching by failures, but in the case of stop-Process, I am going to make an exception.
Instructions:
One more obvious pre-requisite, start Notepad! # clear-Host Learning PointsNote 1: Even if you have started notepad, the following script will not stop the notepad process. # clear-Host Learning PointsNote 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, stop-Process WorkingAll I needed was to pipe 'get-Process notepad'. The result is its output becomes the input for stop-process. Here is the working example: # clear-Host Learning PointsNote 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-ProcessTypical 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 Learning PointsNote 1: The stop-Process is versatile. If you start 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: 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 Learning PointsNote 1: When researching PowerShell commands remember get-Member or (gm). This is how I discovered .kill(). 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. Summary of PowerShell's stop-ProcessAs 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 also more process and service topics• PowerShell Home • get-Process • stop-Process • get-Service • start-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.
*
|
||||||