ngoozeff ngoozeff - 1 year ago 109
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.

Answer Source

You will have to start a process:

start-process powershell -ArgumentList "sleep 10; cmd /c set > c:\env.txt" -WindowStyle hidden
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download