How do I run a PowerShell script when the computer starts?

前端 未结 15 1162
-上瘾入骨i
-上瘾入骨i 2020-11-30 19:24

I have a PowerShell script that monitors an image folder. I need to find a way to automatically run this script after the computer starts.

I already tried the follow

15条回答
  •  春和景丽
    2020-11-30 19:38

    I have a script that starts a file system watcher as well, but once the script window is closed the watcher dies. It will run all day if I start it from a powershell window and leave it open, but the minute I close it the script stops doing what it is supposed to.
    You need to start the script and have it keep powershell open.
    I tried numerous ways to do this, but the one that actually worked was from http://www.methos-it.com/blogs/keep-your-powershell-script-open-when-executed

    param ( $Show )
    if ( !$Show ) 
    {
        PowerShell -NoExit -File $MyInvocation.MyCommand.Path 1
        return
    }
    

    Pasting that to the top of the script is what made it work.
    I start the script from command line with

    powershell.exe -noexit -command "& \path\to\script.ps1"

提交回复
热议问题