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?
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With