Link to home
Start Free TrialLog in
Avatar of IzzyTwinkly
IzzyTwinklyFlag for United States of America

asked on

selecting "A" or "B" in process using Powershell

Hi guys,

I am trying to write my first powershell script.
using powersehll, I want to delete all currently running processesAs and ProcessBs.
I wrote the following code, but it seems that it doesn't end anything.
how can i make this work? (There can be multiple running As and multiple running Bs)

function Kill-RunningProcess(){
        $processesA = @(Get-Process sqlServr -ErrorAction SilentlyContinue)
        $processesB = @(Get-Process winword -ErrorAction SilentlyContinue )
        $array=@($processesA, $processesB)
        if($array){
            foreach($process in $processes){
                $process.Kill()
            }
       }
    }
Avatar of zulazen
zulazen

Try this:

function kill-runningprocess {

$ProcessesA = get-process -name sqlServr -ErrorAction SilentlyContinue
$ProcessesB = get-process -name winword -ErrorAction SilentlyContinue

$ProcessesA | stop-process
$ProcessesB | stop-process

}

Open in new window


or simply

function kill-runningprocess{
get-process -name sqlServr -ErrorAction SilentlyContinue | stop-process
get-process -name winword -ErrorAction SilentlyContinue | stop-process
}

Open in new window

Avatar of oBdA
Get-Process accepts an array for process names to retrieve, so you can do that easily in a single line:
Get-Process -Name sqlServr, winword | Stop-Process

Open in new window

Or like this:
Get-Process -Name sqlServr, winword | ForEach-Object {$_.Kill()}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IzzyTwinkly

ASKER

Hi Qlemo,
Your answer is very simple and clear. Thanks you!

Thanks to oBdA as well!