How kill a process using powershell without getting errors when the process does not exist
Asked Answered
R

3

6

I want to kill the nodepad process if it exists. If I use this:

PS C:\> get-process -name notepad | Stop-Process -Force

I will get an error when the process does not exist.

get-process : Cannot find a process with the name "notepad". Verify the process name and call the cmdlet again.
At line:1 char:1
+ get-process -name notepad | Stop-Process -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (notepad:String) [Get-Process], ProcessCommandException
    + FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand

I feel it should be silent, as it is not an error, the object should be null.

I know I could do:

PS C:\> get-process | where { $_.processname -eq 'notepad' } | Stop-Process -Force

but I prefer a simple way if it exists. Thanks.

PS: I need to kill the notepad because when I non-interactively install ClamAV, it create the notepad with a user manual. I could not find a way to tell ClamAV not to open the user manual.

Rheostat answered 27/4, 2021 at 16:27 Comment(0)
S
10

Just add -ErrorAction SilentlyContinue. This one is simpler and does not prompt any erros if process does not exist.

Get-Process -Name notepad -ErrorAction SilentlyContinue | Stop-Process -Force
Selfhelp answered 27/4, 2021 at 16:33 Comment(0)
N
2

I suggest checking if the notepad process is the one you want first before just force-ending all notepad processes on the system:

Try {
    (Get-WmiObject win32_process -Filter "name='notepad.exe'" | 
    Where commandline -like '*\path\to\manual.txt'
    ).Terminate()
}
# Handle "can't call method on null" error:
Catch [System.SystemException] { "Manual.txt process not found, skipping..." }
Nuriel answered 27/4, 2021 at 16:58 Comment(7)
It is a good idea to check the process. The "process not found" exception should not be needed using pipe.Rheostat
@Rheostat Get-WmiObject doesn't return an error when no processes are found, but the Terminate() method does, so that's what I catch instead.Nuriel
Just a nitpick: the Terminate method is not actually raising the exception. It is the powershell runtime raising the exception when you try to call a method on non-existent objectTael
Another note, you can do this also without wmi with get-process Get-Process notepad -ErrorAction SilentlyContinue | Where-Object CommandLine -like '*\path\to\manual.txt' | Stop-Process -ForceTael
@Tael [System.Diagnostics.Process] objects returned by Get-Process do not have a CommandLine property, as far as I'm aware only wmi (and/or Get-CimInstance) commands add it.Nuriel
They do, at least they do in PS7 Get-Process | Get-MemberTael
@Tael You're right! (I had to upgrade from 7.0.3 to 7.1.3). Note that it's a ScriptProperty that just runs (Get-CimInstance Win32_Process -Filter "ProcessId = $($this.Id)").CommandLine behind the scenes.Nuriel
T
2

or ...

Stop-Process -Name "chrome" -Force -ErrorAction SilentlyContinue

And if you are using this with Gitlab beware that it has a bug with PowerShell runner, that you can work around by encapsulation inside parenthesis resulting in:

(Stop-Process -Name "chrome" -Force -ErrorAction SilentlyContinue)

source: Gitlab powershell issue breaking on ErrorAction

Tallith answered 9/1, 2023 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.