How to Use Start-Process to Detach a PowerShell Process?

0
41
Asked By CuriousCoder92 On

I'm working on a PowerShell script where I use rclone to mount remote file shares. The key commands I've been using include Start-Process with parameters like -PassThru and -NoNewWindow, but I'm having an issue. I thought Start-Process would allow the child process to run independently, so the parent can close without affecting it. However, when I run my script, the parent window stays open indefinitely, and if I close it, the rclone task gets killed too. Is there something I'm misunderstanding about how Start-Process works? Also, I found out from comments that I should be using -WindowStyle Hidden instead of -NoNewWindow. Can someone confirm this?

4 Answers

Answered By PwrShellNinja On

Just a heads-up; if you run a detached process and then close the parent PowerShell window, it can lead to unpredictable results. If the child process stays alive, good luck managing it. Just keep in mind how terminating parents might impact child processes. It's something to consider if your script isn't running under supervision.

Answered By TechieTom81 On

You're correct about the -NoNewWindow parameter being the culprit. When you use that, it keeps the new process tied to your current PowerShell session. If you want a truly detached process, try removing that parameter and see how it behaves.

Answered By ScriptingSam On

Exactly! The -NoNewWindow option prevents your script from detaching from the parent process. Switching to -WindowStyle Hidden should help in running your script without a visible window, which is what you need for detachment.

Answered By MountMaster97 On

You might also want to consider removing the -PassThru argument. This is trying to send output back to your terminal, which might cause it to wait for completion. Removing it might help in closing the parent window without killing the child process.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.