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

Learn how do i run a powershell script when the computer starts? with practical examples, diagrams, and best practices. Covers powershell development techniques with visual explanations.

Automate Your Workflow: Running PowerShell Scripts at Startup

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

Learn various robust methods to automatically execute PowerShell scripts when your Windows computer starts, ensuring critical tasks run without manual intervention.

Automating tasks is a cornerstone of efficient system administration and personal productivity. One common requirement is to have specific scripts run automatically when a computer starts up. PowerShell, with its powerful scripting capabilities, is an excellent tool for this. This article explores several reliable methods to configure PowerShell scripts to execute at system startup, ranging from simple user-specific solutions to more robust, system-wide approaches.

Method 1: Using the Startup Folder (User-Specific)

The simplest way to run a script at startup for a specific user is to place a shortcut to the script in the user's Startup folder. This method is straightforward but only applies to the user who logs in. If the script requires administrative privileges, it will prompt the user for elevation.

1. Locate the Startup Folder

Open the Run dialog (Win + R), type shell:startup, and press Enter. This will open the current user's Startup folder.

2. Create a Shortcut to Your Script

Right-click in the Startup folder, select New > Shortcut. Browse to your PowerShell script (.ps1 file). In the 'Type the location of the item' field, prepend powershell.exe -WindowStyle Hidden -File to the full path of your script. For example: powershell.exe -WindowStyle Hidden -File "C:\Scripts\MyStartupScript.ps1". The -WindowStyle Hidden argument prevents a console window from briefly appearing.

3. Name the Shortcut

Give your shortcut a descriptive name and click Finish. The script will now execute silently each time the user logs in.

Windows Task Scheduler offers a more powerful and flexible way to run scripts at startup. It allows for precise control over execution conditions, user context, and error handling. This is generally the recommended method for production environments or scripts requiring elevated privileges without user interaction.

flowchart TD
    A[Start] --> B["Open Task Scheduler (taskschd.msc)"]
    B --> C["Create Basic Task..."]
    C --> D["Name Task & Description"]
    D --> E["Trigger: 'When the computer starts'"]
    E --> F["Action: 'Start a program'"]
    F --> G["Program/script: 'powershell.exe'"]
    G --> H["Add arguments: '-NoProfile -ExecutionPolicy Bypass -File "C:\Scripts\MyStartupScript.ps1"'"]
    H --> I["Configure Task Settings (Run with highest privileges, etc.)"]
    I --> J[Finish & Test]

Flowchart for setting up a PowerShell script in Task Scheduler.

1. Open Task Scheduler

Press Win + R, type taskschd.msc, and press Enter.

2. Create a New Task

In the right-hand pane, click Create Task... (not Create Basic Task... for more options). Give the task a meaningful Name and Description.

3. Configure General Settings

On the General tab, select Run whether user is logged on or not and check Run with highest privileges. You can also specify the user account under which the script should run.

4. Set the Trigger

Go to the Triggers tab, click New.... From the Begin the task: dropdown, select At startup. Click OK.

5. Define the Action

Go to the Actions tab, click New.... For Action:, select Start a program. In the Program/script: field, type powershell.exe. In the Add arguments (optional): field, enter: -NoProfile -ExecutionPolicy Bypass -File "C:\Scripts\MyStartupScript.ps1" (replace with your script's path). Click OK.

6. Review and Finish

Review the settings on the Conditions and Settings tabs (e.g., Stop the task if it runs longer than:, If the task fails, restart every:). Click OK to save the task. You may be prompted for credentials for the user account specified in the General tab.

$Action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-NoProfile -ExecutionPolicy Bypass -File "C:\Scripts\MyStartupScript.ps1"'
$Trigger = New-ScheduledTaskTrigger -AtStartup
$Settings = New-ScheduledTaskSettingsSet -RunLevel Highest -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit (New-TimeSpan -Hours 2)
$Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
Register-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings -Principal $Principal -TaskName "MyStartupPowerShellScript" -Description "Runs a PowerShell script at system startup."

PowerShell script to create a scheduled task for startup.

Method 3: Group Policy (Domain Environments)

In domain-joined environments, Group Policy Objects (GPOs) provide a centralized and scalable way to deploy startup scripts to multiple computers. This method is ideal for IT administrators managing a large fleet of Windows machines.

1. Open Group Policy Management Editor

On a Domain Controller or a machine with RSAT installed, open gpmc.msc. Navigate to the desired GPO (or create a new one) and right-click to Edit.

2. Navigate to Startup Scripts

In the Group Policy Management Editor, go to Computer Configuration > Policies > Windows Settings > Scripts (Startup/Shutdown) > Startup.

3. Add Your PowerShell Script

Double-click Startup, then click Add.... In the Script Name field, type powershell.exe. In the Script Parameters field, enter: -NoProfile -ExecutionPolicy Bypass -File "%~dp0MyStartupScript.ps1". The %~dp0 variable refers to the directory where the script itself resides, which is useful when deploying scripts via GPO. You'll need to place your script in the GPO's Scripts\Startup folder (e.g., \\Domain\SYSVOL\Domain\Policies\{GUID}\Machine\Scripts\Startup).

Ensure the GPO is linked to the appropriate Organizational Unit (OU) containing the target computers. The script will run when the computers in that OU start up and apply the GPO.

Choosing the Right Method

The best method depends on your specific needs:

  • Startup Folder: Quick and easy for personal, user-specific tasks that don't require elevated privileges or robust error handling.
  • Task Scheduler: Highly recommended for single machines or a few machines where you need fine-grained control, elevated privileges, and reliable execution. It's more robust than the Startup folder.
  • Group Policy: Essential for enterprise environments to deploy scripts consistently across many domain-joined computers. Offers centralized management and execution in the system context.