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

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 following methods, but I couldn't get it working.

  1. Use msconfig and add the PowerShell script to startup, but I cannot find the PowerShell script on that list.

  2. Create a shortcut and drop it to startup folder. No luck.

    %SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"
    

    or

    %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"
    

    Here's my PowerShell script:

    $folder = "C:\\Doc\\Files"
    $dest = "C:\\Doc\\Files\\images"
    $filter = "*.jpg"
    
    $fsw = new-object System.IO.FileSystemWatcher $folder, $filter -Property @{
        IncludeSubDirectories=$false
        NotifyFilter = [System.IO.NotifyFilters]'FileName, LastWrite'
    }
    
    $onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
    
        Start-Sleep -s 10
        Move-Item -Path C:\Doc\Files\*.jpg C:\Doc\Files\images
    }
    
  3. I also tried to add a basic task using taskschd.msc. It is still not working.

    Here's what I found, and maybe that will help to debug it.

    If I open up a PowerShell window and run the script there, it works. But if I run it in a command prompt,

    powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"
    

    It will not work. I am not sure it's a permission problem or something else.

    BTW, I have PowerShell 3.0 installed, and if I type $host.version, it will show 3 there. But my powershell.exe seems like it is still v1.0.

    %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
    

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:

%USERPROFILE%\Desktop\script.ps1

  1. Create a .cmd file and place it in

%AppData%\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 %USERPROFILE%\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.


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


What I do is create a shortcut that I place in shell:startup.

The shortcut has the following:

Target: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "C:\scripts\script.ps1"

(replacing scripts\scripts.ps1 with what you need)

Start In: C:\scripts

(replacing scripts with folder which has your script)


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"

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.