Automating PowerShell Tasks Without the Console Flash

If you're using PowerShell regularly, you’ll likely want to automate tasks to run at scheduled intervals. Scheduling tasks in Windows is easy with PowerShell, but there’s a catch: PowerShell is a console application, and when you run it, the console window flashes on screen—even if you use the -WindowStyle Hidden flag. This can be distracting if your task runs frequently.

Let’s break down how you can automate PowerShell tasks and avoid that annoying console window flash, focusing on the most modern solution.

Running PowerShell Tasks from the Command Line

First, to run a PowerShell task from the command line, you can simply enter:

C:\>powershell -command "write-host test"

This will print the word "test" to the console. If you don’t want your custom profile to load, use the -NoProfile parameter:

C:\>powershell -NoProfile -command "write-host test"

Scheduling a Task with Task Scheduler

Let’s say you want to log the date to a file every minute. You can use the following command:

C:\>schtasks /Create /SC MINUTE /TN schtest /TR "powershell -command 'get-date | add-content c:\temp\test.txt'"

However, the console window will still appear each time this task runs. What can we do about that?

Modern Solution: Use conhost.exe --headless

On newer versions of Windows, you can avoid the console flash entirely by using conhost.exe in headless mode. This spawns PowerShell without ever showing the console window:

C:\>conhost.exe --headless powershell.exe -command 'get-date | add-content c:\temp\test.txt'

Sample for running a script:

C:\>conhost.exe --headless powershell.exe -File "Script.ps1"

For running a script with arguments:

C:\>conhost.exe --headless powershell.exe -File "Script.ps1" "Argument1" "Argument2"

This is a great modern solution as it completely eliminates the console flash without needing additional scripts or executables which keeps your system safe.

Other Solutions (If Not Using Newer Windows)

If you’re on an older version of Windows, or if for some reason conhost.exe isn't available, there are a few workarounds:

  • Use a VBS Script: You can use a Visual Basic Script (VBS) to hide the console window. Here’s an example script:
Set objShell = CreateObject("WScript.Shell")
path = WScript.Arguments(0)
command = "powershell -noprofile -windowstyle hidden -executionpolicy bypass -file """ & path & """"
objShell.Run command, 0, True

You would schedule this script using:

c:\>schtasks /Create /SC MINUTE /TN schtest /TR "wscript c:\Hide-PSWindow.vbs c:\File.ps1"

  • Use a Third-Party Tool: You can use a separate executable to hide the console, but I do not recommend doing so unless it has been developed in-house and is signed with a valid Code-Signing certificate for security purposes.

Conclusion

For those running newer versions of Windows, the best way to schedule hidden PowerShell tasks is to use conhost.exe --headless. This method ensures PowerShell runs quietly in the background without any flashing windows. If you’re on an older version of Windows, a simple VBS script can also get the job done.

Comments