How to Run a Command on Windows Startup (Before User Login)


40 views

When managing multiple Windows servers, you often need to execute commands during system startup - before any user logs in. This is particularly useful for non-persistent configuration changes or service initializations that must occur early in the boot process.

The most reliable way is using Task Scheduler with these settings:

1. Create a new task with highest privileges
2. Set trigger to "At startup"
3. Set action to "Start a program"
4. Configure the program path and arguments
5. Select "Run whether user is logged on or not"

Example command to create a task via command line:

schtasks /create /tn "StartupCommand" /tr "C:\path\to\your\command.exe -param1 -param2" /sc onstart /ru SYSTEM

For simpler cases, you can use these registry locations:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run

Example PowerShell script to add a registry entry:

New-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Run" 
-Name "MyStartupCommand" -Value "C:\scripts\startup.bat" -PropertyType String

For enterprise environments, Group Policy is ideal:

1. Open Group Policy Management
2. Navigate to: Computer Configuration > Preferences > Control Panel Settings > Scheduled Tasks
3. Create a new scheduled task with "At system startup" trigger
4. Deploy to all target servers

If your command needs to run after certain services start, add a delay:

# PowerShell delay script example
Start-Sleep -Seconds 30
& "C:\scripts\delayed_startup.ps1"

Check execution logs with:

Get-WinEvent -LogName "Microsoft-Windows-TaskScheduler/Operational" | 
Where-Object {$_.Id -eq 100} | Format-List

When dealing with Windows servers, it's crucial to distinguish between commands that run:

  • At system boot (before any user logs in)
  • During user login (after credentials are entered)

For persistent configuration changes across reboots, you need boot-time execution, not login-triggered execution.

The most reliable approach for modern Windows systems (Server 2012 R2 and newer):

# PowerShell command to create a boot-triggered task
$action = New-ScheduledTaskAction -Execute "C:\Path\to\your\batch.bat"
$trigger = New-ScheduledTaskTrigger -AtStartup
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -DontStopOnIdleEnd
Register-ScheduledTask -TaskName "BootConfigUpdater" -Action $action -Trigger $trigger -Settings $settings -User "SYSTEM" -RunLevel Highest

Key advantages:

  • Runs under SYSTEM context (no login required)
  • Supports complex triggers and conditions
  • Provides logging through Event Viewer

For older systems where Task Scheduler isn't available:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunServices]
"BootConfig"="C:\\Path\\to\\your\\executable.exe -param1 -param2"

Important considerations:

  • Use RunServices instead of Run for pre-login execution
  • Paths must be absolute and properly escaped
  • Limited visibility compared to Task Scheduler

For temporary changes that shouldn't survive reboots, consider these patterns in your batch file:

@echo off
:: Example boot-time configuration script
reg add "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" /v "SomeTemporaryValue" /t REG_DWORD /d 1 /f

:: Alternative approach using netsh
netsh interface ipv4 set global ecncapability=restricted

To confirm your command ran successfully:

# Check Task Scheduler history
Get-WinEvent -LogName "Microsoft-Windows-TaskScheduler/Operational" | Where-Object {$_.Message -like "*BootConfigUpdater*"}

# Alternative: Create verification file
@echo off
echo %date% %time% >> C:\BootExecution.log