3

I have tried to run the script using command

cmd.exe /c Start /min powershell.exe -windowstyle hidden -file <file>.ps1

But getting a CMD window for a fraction of a second. I need it to run completely hidden.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
v_b
  • 175
  • 1
  • 4
  • 10

2 Answers2

2

Configure the scheduled task to run whether the user is logged on or not:

enter image description here

and reduce the commandline to this:

powershell.exe -File "C:\path\to\your.ps1"

This makes the task run in the background with no visible window.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • You may need to add yourself to Backup Operators group for this to work because running a task in the background under your account(If the script requires higher privilages) will require the Run Batch permission. I solved the problem slightly differently from this article by adding myself to Backup Operators: https://danblee.com/log-on-as-batch-job-rights-for-task-scheduler/ – AaronLS Jan 11 '19 at 20:20
1

I have had this issue and the only way I could fix it was to call the PowerShell script with a simple VBS wrapper:

https://github.com/gbuktenica/PsRun

' SYNOPSIS
'   Run a PowerShell script in the user context without a script window
' EXAMPLE
'   wscript.exe PsRun.vbs MyPsScript.ps1
' AUTHOR
'   Glen Buktenica

Set objShell = CreateObject("Wscript.Shell")
Set args = Wscript.Arguments
For Each arg In args
    Dim PSRun
    PSRun = "powershell.exe -WindowStyle hidden -ExecutionPolicy bypass -NonInteractive -File " & arg
    objShell.Run(PSRun),0
Glen Buktenica
  • 302
  • 2
  • 12