Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell kill multiple processes

I am trying to accomplish the following via PS and having an issue getting what I need. I have tried many different formats for writing this script and this is the closest I have gotten I think.

I run the following and no error, but no result either.

$softwarelist = 'chrome|firefox|iexplore|opera'
get-process |
Where-Object {$_.ProcessName -eq $softwarelist} |
stop-process -force

Here is another example that I was trying, but this one wasn't terminating all the process I was providing (IE in W8.1).

1..50 | % {notepad;calc}

$null = Get-WmiObject win32_process -Filter "name = 'notepad.exe' OR name = 'calc.exe'" |

% { $_.Terminate() }

Thanks for any help!

like image 759
skrap3e Avatar asked Dec 04 '22 05:12

skrap3e


2 Answers

Your $softwarelist variable looks like a regular expression, but in your Where-Object condition, you're using the -eq operator. I think you want the -match operator:

$softwarelist = 'chrome|firefox|iexplore|opera'
get-process |
    Where-Object {$_.ProcessName -match $softwarelist} |
    stop-process -force

You can also pass multiple processes to Get-Process, e.g.

Get-Process -Name 'chrome','firefox','iexplore','opera' | Stop-Process -Force
like image 118
Aaron Jensen Avatar answered Dec 06 '22 20:12

Aaron Jensen


# First, create an array of strings.
$array =  @("chrome","firefox","iexplore","opera")


# Next, loop through each item in your array, and stop the process.
foreach ($process in $array)
{
    Stop-Process -Name $process
}
like image 38
SQone2 Avatar answered Dec 06 '22 19:12

SQone2