Powershell could not terminate Excel


I have a problem with powershell which sometimes fails to kill a task or thread.

The error message is as follows:
"The process “EXCEL.EXE” with PID 37108 could not be terminated.
When I test on the same command:

  • If Excel is not running at all, there’s no issue
  • If Excel is running, it will be terminated without problem
  • If I put it in a loop. For instance open and close Excel 100 times. 1 or 2 times, there’s a error thrown.

Absolutely no idea what is causing it.
Any help?

Thanks a lot!

Hi @hellitonwoo

Have you tried using terminate workflow


Thanks for the quick reply!
I think you misunderstood my post.

I’m not looking to terminate the workflow, but rather how to handle the exception and figure out why powershell would throw exception “randomly” on killing a task. :slight_smile:

Here is the script:
KillProcess_CMD.xaml (7.1 KB)

To make it work, you need to provide the “process name” and “domain”
If you run “System.Security.Principal.WindowsIdentity.GetCurrent().Name”, you will get the domain name in format “domain\username”.