Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run PowerShell Script from Task Scheduler without Window

I'm running an Encoded powershell command from Task Scheduler on user login. The command does some evaluations and will present a message box to the user. The issue I'm having is the PowerShell window still flashes no matter what switches I use. Note: the script itself works fine.

This is what I have in task scheduler:

powershell.exe -WindowStyle Hidden -executionpolicy bypass -noninteractive -encodedcommand "COMMAND"

Is there something I'm missing here?

like image 779
Daniel Avatar asked Dec 22 '25 02:12

Daniel


1 Answers

  • powershell.exe is a Windows console-subsystem application (as is pwsh.exe, the PowerShell (Core) CLI).

  • Therefore, Windows currently invariably creates an - initially visible - console window for it when it launches, and only afterwards is PowerShell able to honor -WindowStyle Hidden and hide the window[1] - this causes the flashing you see.

    • Note: It is possible to run scheduled tasks fundamentally invisibly, by making them run in the same invisible session that Windows services run, such as running as NT AUTHORITY\SYSTEM or with the Run whether user is logged on or not option, but then you won't be able to interact with the user's desktop, which is what your intent is.
  • This is a long-standing problem, discussed in GitHub issue #3028.

    • For now, the only workaround is to use a GUI-subsystem wrapper application for launching powershell.exe hidden - see the bottom section of this answer for concrete options.

    • However, a future Windows enhancement, the Console Allocation Policy will offer a solution: conditional allocation of a console, which would allow applications to decide whether or not to allocate a console for themselves based on runtime conditions.
      It is unclear to me when this feature will be available (or even if it is guaranteed to be implemented, but I expect it will be).

      • Since using this feature will require opt-in via an application manifest, the PowerShell CLI will have to be updated to support that.
      • Given that Windows PowerShell is no longer actively maintained, unlike PowerShell (Core) 7+, only the latter's CLI, pwsh.exe, is likely to be updated.

[1] If Windows Terminal is the default terminal application, the resulting window is currently only minimized, which is problematic - see WT GitHub issue #12464.

like image 67
mklement0 Avatar answered Dec 25 '25 08:12

mklement0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!