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.
Just add
-ErrorAction SilentlyContinue
. This one is simpler and does not prompt any erros if process does not exist.