4

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.

puravidaso
  • 1,013
  • 1
  • 5
  • 22

3 Answers3

9

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
Michal Rosenbaum
  • 1,801
  • 1
  • 10
  • 18
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..." }
Cpt.Whale
  • 4,784
  • 1
  • 10
  • 16
  • It is a good idea to check the process. The "process not found" exception should not be needed using pipe. – puravidaso Apr 27 '21 at 17:02
  • @puravidaso `Get-WmiObject` doesn't return an error when no processes are found, but the `Terminate()` method does, so that's what I catch instead. – Cpt.Whale Apr 27 '21 at 17:09
  • 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 object – Daniel Apr 27 '21 at 18:42
  • 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 -Force` – Daniel Apr 27 '21 at 19:00
  • @Daniel `[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. – Cpt.Whale Apr 27 '21 at 20:01
  • 1
    They do, at least they do in PS7 `Get-Process | Get-Member` – Daniel Apr 27 '21 at 20:04
  • 1
    @Daniel 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. – Cpt.Whale Apr 27 '21 at 20:27
0

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

Thierry Brémard
  • 625
  • 1
  • 5
  • 14