Run a powershell script in the background once per minute Run a powershell script in the background once per minute windows windows

Run a powershell script in the background once per minute


Use the Windows Task Scheduler and run your script like this:

powershell -File myScript.ps1 -WindowStyle Hidden

Furthermore create the script that it runs under a specific user account and not only when that user is logged on. Otherwise you'll see a console window.


Perhaps this scenario will do. We do not start PowerShell executable every minute (this is expensive, BTW). Instead, we start it once by calling an extra script that calls the worker script once a minute (or actually waits a minute after the worker script exits or fails).

Create the starting Invoke-MyScript.ps1:

for(;;) { try {  # invoke the worker script  C:\ROM\_110106_022745\MyScript.ps1 } catch {  # do something with $_, log it, more likely } # wait for a minute Start-Sleep 60}

Run this from Cmd (e.g. from a startup .bat file):

start /min powershell -WindowStyle Hidden -Command C:\ROM\_110106_022745\Invoke-MyScript.ps1

The PowerShell window appears for a moment but it is minimized due to start /min and just in a moment it gets hidden forever. So that actually only the task bar icon appears for a moment, not the window itself. It's not too bad.


Schedule your task to be run as System. The command below will schedule a script to be run in the background without showing powershell console window:

schtasks /create /tn myTask /tr "powershell -NoLogo -WindowStyle hidden -file myScript.ps1" /sc minute /mo 1 /ru System

/ru switch lets you change the user context in which the scheduled task will be executed.