This post is about stopping a particular process or processes using PowerShell
script. I often use this script to shutdown processes from shell.
A straightforward way to achieve this is as follows :
stop-process [pid]
However, above command requires the knowledge of the process id, which can be retrieved using get-process PowerShell
command. However, if the process id is not known at this point, the following command can also do the job.
stop-process -processname [pname]
In addition , we could stop all the processes the matches certain criteria. For the sake of simplicity, let’s consider that we would like to stop the processes whose name matches with following “*.chrome” pattern. get-process returns all the processes currently active in the system. Then we filter the processes with names that matches the specified pattern using the Where-Object as follows.
get-process | ?{$_.ProcessName -like "[pattern]"}
For instance, if we use *.chrome as the pattern, currently it is giving following output in my shell :
Next, we perform projection on the returned objects to only select ProcessName and use a iterator to iterate over it and invoke stop-process as follows :
get-process | ?{$_.ProcessName -like "[pattern]"} | %{$_.ProcessName}| foreach {stop-process -ProcessName $_ ; echo $_}
Synopsis. Above code snippet stops all the processes whose name matches with the specified pattern and lists the name of the stopped processes. It went through several steps: filtering , projection and , finally iterating and stopping all the projected process names.
Hope it helps. Cheers!