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

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

    This is really just an expansion on @mjolinor simple answer [Use Task Scheduler].

    I knew "Task Scheduler" was the correct way, but it took a bit of effort to get it running the way I wanted and thought I'd post my finding for others.

    Issues including:

    • Redirecting output to logs
    • Hiding the PowerShell window

    Note: You must have permission to run script see ExecutionPolicy

    Then in Task Scheduler, the most important/tricky part is the Action

    It should be Start a Program

    Program/Script:

    powershell
    

    Add arguments (optional) :

    -windowstyle hidden -command full\path\script.ps1 >> "%TEMP%\StartupLog.txt" 2>&1

    Note:

    If you see -File on the internet, it will work, but understand nothing can be after -File except the File Path, IE: The redirect is taken to be part of the file path and it fails, you must use -command in conjunction with redirect, but you can prepend additional commands/arguments such as -windowstyle hidden to not show PowerShell window.

    I had to adjust all Write-Host to Write-Output in my script as well.

提交回复
热议问题