1

I have a PS script initialized by Windows Task Scheduler that runs every five minutes. I've noticed when it initiates, the command prompt opens up briefly then goes away (the very nature of the script itself which is understandable). However, I want to hide it in the background because every five minutes the command prompt comes up and can be obstructive. I've tried to check the checkbox "hidden" in Windows Task Scheduler, but the result is the script will not run.

I have also tried writing -Hidden in the arguments field of the Task Scheduler properties. The script will not run.

Is there a line of code that will enable my PS script to run in the background and not obstruct usage of the desktop environment?

I am not interested in wrapping it in .bat or VB script. I am curious if there is a line of PS code I can add to universally work with any .ps1. I am aware of a VB script solution provided below and if last resort, will have to use it:

https://www.sapien.com/blog/2006/12/20/schedule-hidden-powershell-tasks/

Lasagna Cat
  • 297
  • 3
  • 24

2 Answers2

2

The short answer is no. You'll always get the PowerShell window at least flash momentarily (such as with -WindowStyle Hidden) because of the way it's executed. The answer is, unfortunately, one of the solutions you're not interested in: wrap it in an executable or launch it with the VB script below.

Dim shell,command
command = "powershell.exe -nologo -command \\path-to\your-script.ps1"
set shell = CreateObject("WScript.Shell")
shell.Run command,0'
InfosecSapper
  • 129
  • 1
  • 4
  • Actually, no. If you set the task scheduler and you see the "hidden" radial you CAN hide the command. – Patrick Burwell Aug 11 '21 at 18:54
  • 1
    @PatrickBurwell - If you mean the 'Hidden' checkbox in the bottom left corner of the Task Properties window, that's to hide the task in the Task Scheduler window. In Task Scheduler, go to View>Show Hidden Tasks to see what I mean. – InfosecSapper Aug 12 '21 at 21:29
  • When we use the the Hidden radial in the Scheduled Task and the hidden WindowStyle options the jobs NEVER show when you are logged on and run the scheduled task manually. PLUS, we use SVC accounts to run them (not interactive accounts), so we make sure they don't show, ever. ;) Hope that is helpful to someone. – Patrick Burwell Oct 09 '21 at 18:56
-1

Answer:

"Add Arguments (optional)" field

-nologo -file "path\file.ps1" -WindowStyle Hidden -ExecutionPolicy Bypass

The -nologo' and '-ExecutionPolicy Bypass' options are self-explanatory

Patrick Burwell
  • 129
  • 1
  • 12
  • It would be better to answer the question this one is a duplicate of (the duplicate was named in comments long before you wrote this answer, I just promoted it to a full duplicate). Except that these arguments and the fact that they don't solve the problem are already well discussed there. – Ben Voigt Aug 30 '21 at 18:12