3

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!

0

2 Answers 2

3

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'
Sign up to request clarification or add additional context in comments.

Comments

-1

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"'

1 Comment

By the time powershell.exe sees -windowstyle hidden, it has already been launched in a visible console window; use Start-Process' own -WindowStyle parameter instead.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.