ngoozeff ngoozeff - 1 month ago 7x
PowerShell Question

How can I start a background job in Powershell that outlives it's parent?

Consider the following code:

start-job -scriptblock { sleep 10; cmd /c set > c:\env.txt; }

The background job is killed when the parent exits, so the call to cmd.exe never occurs. I would like to write some similar code, such that the parent exits immediately, and the child continues to run in the background.

I would like to keep everything in one script if possible.


You will have to start a process:

start-process powershell -ArgumentList "sleep 10; cmd /c set > c:\env.txt" -WindowStyle hidden