PowerShell | Stopping processes using a PowerShell script

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!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s