how to stop powershell invoker in C#

Question:

I’m invoking powershell command with C#, and powershell command is invoked background. and i want to terminate the background thread. everytime, i terminate the background thread, the powershell is still running which result in that i can’t run that thread again. is there any method to terminate powershell execution?

the background thread as follows:

Answer:

Stopping a PowerShell script is pretty simple thanks to the Stop() method on the PowerShell class.

It can be easily hooked into using a CancellationToken if you want to invoke the script asynchronously:

Source:

how to stop powershell invoker in C# by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply