7

I am trying to run a PowerShell script using Task Scheduler and it pops up a PowerShell command console window.

Is there a way to disable this while running the script and hide the window?

I've tried -WindowStyle Hidden but the PowerShell command console window still pops up.

2 Answers 2

12

Since powershell.exe is a console program, you can't execute it normally without its console window appearing (although you can hide it shortly after it starts by using -WindowStyle Hidden, as you have noted).

The trick is to execute powershell.exe itself in a hidden state. One way to do this is by using the WshShell object's Run method to run a PowerShell command line as hidden from inside a WSH script, and execute the WSH script using wscript.exe (which is not a console program, so no console window appears). Example script (JScript):

var wshShell = new ActiveXObject("WScript.Shell");
wshShell.Run('%SystemRoot%\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -ExecutionPolicy Bypass -File "C:\\Scripts\\My Script.ps1"', 0, false);

If this script is C:\Scripts\My Launcher.js, you can now run the following command line:

wscript "C:\Scripts\My Launcher.js"

This runs the launcher script without a console, which then runs the PowerShell command in a hidden window (the Run method's second parameter is 0).

Sign up to request clarification or add additional context in comments.

1 Comment

For a re-usable VBS script, see this answer: stackoverflow.com/a/51007810/9803922
1

If you set the task to "Run only when the user is logged on", then the script will be launched in the context of the user's desktop session. That allows you to see and interact with the console window.

When you have it set to "Run whether the user is logged on or not." then the launched script does not interact with the desktop. You cannot "see" the window.

enter image description here

You can also compile the script to an exe with the "noConsole" option with PS2EXE

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.