1

When launching PowerShell.exe as a scheduled task with the options -WindowStyle Hidden -NoProfile -Command <cmd>, the blue PowerShell window opens in the blink of an eye and then dissapears (runs as a background process).

How can I prevent the blue PowerShell window from appearing in a split second, which can lead to confusion for the end-user?

Shuzheng
  • 11,288
  • 20
  • 88
  • 186
  • This has been asked before. https://stackoverflow.com/questions/1802127/how-to-run-a-powershell-script-without-displaying-a-window – js2010 May 27 '19 at 13:50
  • Possible duplicate of [How to run a PowerShell script without displaying a window?](https://stackoverflow.com/questions/1802127/how-to-run-a-powershell-script-without-displaying-a-window) – Matthew May 27 '19 at 14:32
  • None of these answers solve my issue. They all depend on 3-party applications, and the -LogonType S4U` doesn't work in my case. – Shuzheng May 28 '19 at 06:24

1 Answers1

0

On the General tab of the scheduled task, select "Run whether user is logged on or not" to ensure the task does not pop up a window.

Run whether user is logged on or not

To do the same thing in PowerShell, use -LogonType S4U for the task principal configuration. Example below:

$action = New-ScheduledTaskAction -Execute notepad.exe
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1)
$principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType S4U -RunLevel Highest

Register-ScheduledTask -TaskName "Test1337" -TaskPath "\" -Action $action -Trigger $trigger -Principal $principal

Tested and confirmed on Windows Server 2016 build 1607 (and many other versions by way of previous deployments using this same method).

Jay Adams
  • 2,052
  • 1
  • 12
  • 7
  • Thank you, but how can I select this option, when registering the scheduled task from PowerShell? – Shuzheng May 28 '19 at 17:37
  • Answer has been updated with PowerShell example using -LogonType S4U to accomplish the same thing. – Jay Adams May 28 '19 at 18:18
  • Ahh, so in this case the task cannot run under the user account who invokes the PowerShell command? Usually, the task has been registered without supplying credentials for the invoking user account. – Shuzheng May 28 '19 at 19:03