powershellpidkillkill-processstart-process

PowerShell, Start an app hidden in background, then kill that app


An app I want to push to various systems (Notepad++) doesn't generate it's configuration file until after it starts, so I'd like to start the application quickly hidden in the background so that the config.xml is generated (grabbing the apps PID as it is starting), then kill the app a second later. I thought I could do this as follows:

Start-Process -WindowStyle Hidden -FilePath "C:\Program Files\Notepad++\notepad++.exe

But this does not work and the app opens normally. How can I start this app hidden in the background? Also, how can I get the PID as it is starting so that I can kill it shortly afterwards?


Solution

  • I don't think you can start an app like notepad++ without a window, it's a GUI application. As for the other question, you are looking for the PassThru switch. Some cmdlets do not pass the output to the pipeline if this is not specified So you can do something like this:

    $process = Start-Process -FilePath 'C:\Program Files\Notepad++\notepad++.exe' -PassThru
    Stop-Process -Id $process.Id