Run PowerShell Script at Startup

Run PowerShell script at Start up

Our script will open the Windows Calculator application at Windows startup and login based on the solution you will choose from. To meet your requirements, you can replace the script in file.ps1 with your script.

Using Startup Folder

Use the Startup folder to run the PowerShell script at startup. To do this, we must follow this section’s step-by-step instructions.

Create .ps1 File

Open Notepad, write the following command, and save the file with any name of your choice but with the .ps1 extension. You can store this PowerShell script file at any location on your local machine; we saved this file with the file.ps1 name at the E:\Test location.

This command would open the calculator on the local computer.

Create .cmd File

Launch Notepad, write the following commands, and save the file with a name but with the extension .cmd at %AppData%\Microsoft\Windows\Start Menu\Programs\Startup path. We named this file Startup.cmd and placed it in the Startup Folder.

While saving the .cmd file, don’t forget to select All Files for the Save as type: option and ANSI for the Encoding option; see the screenshot after the following script for clear understanding.

run powershell script at startup - save cmd file

The above script did two things. First, it sets the execution policy to Unrestricted, and the second is to use PowerShell to run the .ps1 script file in the given path. Let’s break down the above commands to learn them in detail.

The first command:

  1. Runs the PowerShell with the -Command parameter to execute the Set-ExecutionPolicy Unrestricted command, which modifies the PowerShell execution policy to let us run unsigned scripts.
  2. The -Scop parameter applies changes to the current user instead of the entire system.
  3. The redirection operator represented by >> redirects the command output (if any) to the StartupLog.txt file in the user’s %TEMP% directory. The >> operator appends the output to the end of the StartupLog.txt file if it already exists; otherwise, it creates it.
  4. The 2>&1 operator redirects the command’s error output (2) to the exact file as the standard output (&1).

The second command:

  1. Runs PowerShell with the E:\Test\file.ps1 script as an argument. You might have a different path if you have stored your .ps1 file in a different location on your PC.
  2. The redirection operator (>>) does the same as in the first command.
  3. The 2>&1 operator also does the same thing as in the first command.

Overall, both commands use the >> and 2>&1 operators to capture the errors and output of the PowerShell command or script and write them to a StartupLog.txt file in the user’s %TEMP% directory.

Restart Your PC

Now, you have the .ps1 file and .cmd file at the required locations, so it’s time to restart your computer. Once the computer is on, the script will be executed and open the Calculator app for you.

Using Task Scheduler

Use Task Scheduler to run PowerShell script at startup. Following the below steps to do it.

Open Task Scheduler

Press Windows+R key from the keyboard, type taskschd.msc in the Run prompt and hit OK. It will open the Task Scheduler window for you. See the following screenshot.

run powershell script at startup - open task scheduler

Otherwise, you can use the Windows search menu to open Task Scheduler.

Create a Task
  1. In the Task Scheduler window, click Create Task as demonstrated below.

    run powershell script at startup - create task

  2. Ensure you are on the General tab in the Create Task window. Then, write the name of the task (step-1), select the Run only when the user is logged on radio button (step-2), check the Run with highest privileges checkbox (step-3), and click on the Actions tab (step-4).

    run powershell script at startup - name task

  3. Once you are on the Actions tab, click on the New button (step-2), make sure the Start a program option is selected for Actions: option (step-3), write powershell.exe for Program/script option (step-4), add E:\Test\file.ps1 as the value of Add arguments (optional) option (step-5) and click OK button (step-6). Remember, you can specify your arguments for step-5.

    run powershell script at startup - new action

  4. Now, click on the Triggers tab (step-1), click on the New button (step-2), select At log on for Begin the task: option (step-3), choose the Any user or Specific user based on your requirements (step-4), click OK (step-5), and click OK again (step-6).

    run powershell script at startup - create trigger

    Your task must be listed in the Task Scheduler window (see the following); if it is there, you have done everything right, close this window.

    run powershell script at startup - close task scheduler

Log Out and Login Your PC

Log out and log in again to your computer. You don’t need to reboot the computer to run this script. This script will open the Windows Calculator application.

Using Registry Editor

Use Task Scheduler to run PowerShell script at startup. Following the below steps to do it.

Open Registry Editor

Press Windows+R key from the keyboard, type regedit.msc in the Run prompt and hit OK. It will open the registry editor window.

run powershell script at startup - open registry editor

Add Entry to the Windows Registry Editor
  1. Navigate to the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run in the registry editor (step-1).

  2. Right-click the Run key and choose New->String Value (step-2).

  3. Give a new entry a meaningful name as we named it PSfile. Note that this name must be unique; otherwise, you will get an error.

    run powershell script at startup - add entry to registry

  4. Double-click on the new entry (PSfile) to open the Edit String window.

  5. In the Edit String window, you can confirm the new entry’s name (step-3). For the Value data field, enter the powershell "E:\Test\file.ps1" command (step-4); you can replace the path to point your PowerShell script file. Remember to surround the path with double quotes.

  6. Click OK to exit the Edit String window (step-5).

  7. Close the Registry Editor.

Restart Your PC

Restart your computer to run the script automatically every time Windows starts up. This script will open the Windows Calculator application for you.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *