I often run Powershell processes on my server and find the CPU usage spiking up because of this unacceptable.
Some background info:
I have an application (let's call it management app, to avoid confusion) that can run bat/batch files after an event occurs. For example when the management app tries to close an application I make sure that the application is actually forced closed and that
all it's subprocesses are closed as well.
I used to do this with CMD, but since PowerShell is the future and more and more CMD functionalities are being deprecated I decided to switch to PowerShell for this.
The problem:
I use the following commands to launch PowerShell from the management app's Batch Script engine:
powershell -command "& {Get-process | ?{$_.path -like '%Directory%application1.exe"'}}" | Stop-Process -Force"
powershell -command "& {Get-process | ?{$_.path -like '%Directory%application2.exe"'}}" | Stop-Process -Force"
powershell -command "& {Get-process | ?{$_.path -like '%Directory%application3.exe"'}}" | Stop-Process -Force"
Now the scripts work fine, but unfortunately starting PowerShell is relatively slow and causes CPU usage spikes (uses 12% for a few seconds, one full CPU core).
1. I'd like to reduce the CPU usage of PowerShell during it's startup as much as possible and if possible reduce the startup time.
2. Additionally I'm wondering if it's possible to combine the above commands so that only one PowerShell process has to be started to execute all of them.
Some background info about solutions I've tried:
I'm using Windows Server 2012 R2 and have upgraded PowerShell to version 5.0 to see if this would help, but unfortunately it didn't make any difference.
Additionally I came across someone who had the same problem: https://powertoe.wordpress.com/2012/06/27/how-to-execute-powershell-scripts-without-the-cpu-hit-to-start-powershell/
I don't think his approach would work for my situation, since the application I use automatically starts and stops PowerShell when an event is triggered inside the management app.