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

前端 未结 15 1143
-上瘾入骨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:34

    You could create a Scheduler Task that runs automatically on the start, even when the user is not logged in:

    schtasks /create /tn "FileMonitor" /sc onstart /delay 0000:30 /rl highest /ru system /tr "powershell.exe -file C:\Doc\Files\FileMonitor.ps1"
    

    Run this command once from a PowerShell as Admin and it will create a schedule task for you. You can list the task like this:

    schtasks /Query /TN "FileMonitor" /V /FO List
    

    or delete it

    schtasks /Delete /TN "FileMonitor"
    
    0 讨论(0)
  • 2020-11-30 19:38

    Be sure, whenever you want PowerShell to run automatically / in the background / non-interactive, it’s a good idea to specify the parameters -ExecutionPolicy Bypass to PowerShell.exe

    PowerShell.exe -ExecutionPolicy Bypass

    0 讨论(0)
  • 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"

    0 讨论(0)
  • 2020-11-30 19:38

    Try this. Create a shortcut in startup folder and iuput

    PowerShell "&.'PathToFile\script.ps1'"
    

    This is the easiest way.

    0 讨论(0)
  • 2020-11-30 19:39

    You could set it up as a Scheduled Task, and set the Task Trigger for "At Startup"

    0 讨论(0)
  • 2020-11-30 19:42

    Execute PowerShell command below to run the PowerShell script .ps1 through the task scheduler at user login.

    Register-ScheduledTask -TaskName "SOME TASKNAME" -Trigger (New-ScheduledTaskTrigger -AtLogon) -Action (New-ScheduledTaskAction -Execute "${Env:WinDir}\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument "-WindowStyle Hidden -Command `"& 'C:\PATH\TO\FILE.ps1'`"") -RunLevel Highest -Force;
    

    -AtLogOn - indicates that a trigger starts a task when a user logs on.

    -AtStartup - indicates that a trigger starts a task when the system is started.

    -WindowStyle Hidden - don't show PowerShell window at startup. Remove if not required.

    -RunLevel Highest - run PowerShell as administrator. Remove if not required.

    P.S.

    If necessary execute PowerShell command below to enable PowerShell scripts execution.

    Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy Unrestricted -Force;
    

    Bypass - nothing is blocked and there are no warnings or prompts.

    Unrestricted - loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the internet, you're prompted for permission before it runs.

    0 讨论(0)
提交回复
热议问题