Windows Batch Auto-Run (Short Practical Guide)

Use different methods depending on when the script must run.

1) Auto-run after user login (Startup folder)

Create C:\Scripts\hello-login.bat:

@echo off
echo Login script ran at %date% %time% >> C:\Scripts\login.log

Open Startup folder:

Win + R -> shell:startup

Put a shortcut to hello-login.bat there.

Best for per-user, interactive tasks.

2) Auto-run after user login (Registry Run key)

reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" ^
 /v HelloLogin ^
 /t REG_SZ ^
 /d "C:\Scripts\hello-login.bat" ^
 /f

Remove it:

reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v HelloLogin /f

3) Auto-run without login (Task Scheduler)

This is the common method for headless/background execution.

Create task:

schtasks /Create /TN "MyBackgroundJob" ^
 /TR "C:\Scripts\job.bat" ^
 /SC ONSTART ^
 /RU "SYSTEM" ^
 /RL HIGHEST /F
  • /SC ONSTART: runs at boot.
  • /RU SYSTEM: no user login required.
  • /RL HIGHEST: elevated privileges.

Run manually for test:

schtasks /Run /TN "MyBackgroundJob"

4) Run script on user logout

Option A: Local Group Policy (Pro/Enterprise)

Open gpedit.msc:

User Configuration -> Windows Settings -> Scripts (Logon/Logoff) -> Logoff

Add your .bat file there.

Option B: Task Scheduler event trigger

You can trigger on a logout-related event if policy is not available. This is more advanced but works on editions without full GPO tooling.

Practical tips

  • Always log output to a file for debugging.
  • Use full absolute paths in batch files.
  • If your script needs network drives, map them inside the script (do not rely on interactive session mappings).
  • For long-running jobs, prefer Task Scheduler over Startup folder.