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

前端 未结 15 1145
-上瘾入骨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.

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

    This worked for me. Created a Scheduled task with below details: Trigger : At startup

    Actions: Program/script : powershell.exe Arguments : -file

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

    You can see scripts and more scheduled for startup inside Task Manager in the Startup tab. Here is how to add a new item to the scheduled startup items.

    First, open up explorer to shell:startup location via start-button => run:

    explorer shell:startup

    Right click in that folder and in the context menu select a new shortcut. Enter the following:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "C:\myfolder\somescript.ps1"

    This will startup a Powershell script without starting up your $profile scripts for faster execution. This will make sure that the powershell script is started up.

    The shell:startup folder is in:

    $env:APPDATA\Microsoft\Windows

    And then into the folder:

    Start Menu\Programs\Startup

    As usual, Microsoft makes things a bit cumbersome for us when a path contains spaces, so you have to put quotes around the full path or just hit tab inside Powershell to autocomplete in this case.

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

    Copy ps1 into this folder, and create it if necessary. It will run at every start-up (before user logon occurs).

    C:\Windows\System32\GroupPolicy\Machine\Scripts\Startup
    

    Also it can be done through GPEDIT.msc if available on your OS build (lower level OS maybe not).

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

    If you do not want to worry about execution policy, you can use the following and put into a batch script. I use this a lot when having techs at sites run my scripts since half the time they say script didnt work but really it's cause execution policy was undefined our restricted. This will run script even if execution policy would normally block a script to run.
    If you want it to run at startup. Then you can place in either shell:startup for a single user or shell:common startup for all users who log into the PC.

    cmd.exe /c Powershell.exe -ExecutionPolicy ByPass -File "c:\path\to\script.ps1"

    Obviously, making a GPO is your best method if you have a domain and place in Scripts (Startup/Shutdown); under either Computer or User Configurations\Windows Settings\Scripts (Startup/Shutdown). If you go that way make a directory called Startup or something under **

    \\yourdomain.com\netlogon\

    and put it there to reference in the GPO. This way you know the DC has rights to execute it. When you browse for the script on the DC you will find it under

    C:\Windows\SYSVOL\domain\scripts\Startup\

    since this is the local path of netlogon.

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

    I finally got my PowerShell script to run automatically on every startup. You will need to create two files: the first is the Powershell script (e.g. script.ps1) and the second is a .cmd file that will contain commands that will run on the command prompt (e.g. startup.cmd).

    The second file is what needs to be executed when the computer starts up, and simply copy-pasting the .ps1 to the startup folder won't work, because that doesn't actually execute the script - it only opens the file with Notepad. You need to execute the .cmd which itself will execute the .ps1 using PowerShell. Ok, enough babbling and on to the steps:

    1. Create your .ps1 script and place it in a folder. I put it on my desktop for simplicity. The path would look something like this:

    C:\Users\<user_name>\Desktop\script.ps1

    1. Create a .cmd file and place it in

    C:\Users\<user_name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\startup.cmd

    Doing this will execute the cmd file every time on startup. Here is a link of how to create a .cmd file if you need help.

    1. Open the .cmd file with a text editor and enter the following lines:
    PowerShell -Command "Set-ExecutionPolicy Unrestricted" >> "%TEMP%\StartupLog.txt" 2>&1
    PowerShell C:\Users\<user_name>\Desktop\script.ps1 >> "%TEMP%\StartupLog.txt" 2>&1
    

    This will do two things:

    1. Set the Execution Policy of your PowerShell to Unrestricted. This is needed to run scripts or else PowerShell will not do it.
    2. Use PowerShell to execute the .ps1 script found in the path specified.

    This code is specifically for PowerShell v1.0. If you're running PowerShell v2.0 it might be a little different. In any case, check this source for the .cmd code.

    1. Save the .cmd file

    Now that you have your .ps1 and .cmd files in their respective paths and with the script for each, you are all set.

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