How can I launch this PowerShell script in a new hidden window

Viewed 4990

I am working on a Reboot Scheduling application to be used on simple remote machines. It has to be user friendly though so I have to have multiple scripts and tasks in the background. my main GUI script needs to launch a Secondary Script in a NEW hidden window so that the Main GUI Script can close.

the Secondary Script (Reboot.ps1) will run until the machine restarts if the user has a scheduled reboot pending.

The code below starts the Secondary Script hidden but as a sort of "child job", since this script can run "forever" it wont end thus leaving the Main GUI script frozen open until killed in task manager (not very user friendly I know..)

    $program = Powershell.exe -windowstyle hidden -file "C:\Reboot.ps1"
    $scriptblock = $ExecutionContext.InvokeCommand.NewScriptBlock("Invoke-Command {$program}")
    Invoke-Command -NoNewScope -Scriptblock $scriptblock

So what I'm asking is if anyone knows how to start the Secondary Script in a new PowerShell window, instance, environment, anything that allows the Main GUI Script to close. Preferably, less intensive if possible. :) thank you!

2 Answers

Use Start-Process's own -WindowStyle Hidden parameter to launch your script hidden and asynchronously (Start-Process's default):

Start-Process -WindowStyle Hidden powershell.exe -Args '-File', 'C:\Reboot.ps1'

Your code looks really complicated for what you're trying to achieve, use Start-Process

Start-Process powershell.exe -ArgumentList '-windowstyle hidden -file "C:\Reboot.ps1"'
Related