How to Register a Batch Script as a Startup Item in Batch Script
Running a script automatically every time Windows boots is essential for many automation tasks, from launching monitoring tools to setting up a preferred working environment. In this guide, we will explore multiple methods of registering a Batch Script as a Windows startup item, all configurable from within a Batch Script itself.
Whether you are building a system administration toolkit or simply want your daily scripts to run without manual intervention, understanding startup registration is a fundamental skill.
Methods for Auto-Starting Scripts in Windows
Windows provides several mechanisms for running programs at startup:
- The Startup Folder: A special directory where any shortcut placed inside will be executed on logon.
- The Windows Registry: Specific keys in the registry list programs that run at startup.
- Task Scheduler: The most robust option, allowing scheduled tasks with specific triggers and conditions.
Each method has its strengths and trade-offs. We will cover all three.
Method 1: The Startup Folder
The simplest approach is to place a shortcut to your script in the Windows Startup folder.
Locating the Startup Folder
- Current User:
%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup - All Users:
%PROGRAMDATA%\Microsoft\Windows\Start Menu\Programs\Startup
Creating a Shortcut via Batch
Batch Script cannot directly create .lnk shortcut files, but we can use a small VBScript helper to do it, or simply copy the .bat file into the folder.
@echo off
echo Registering script as startup item...
set "script_path=%~dp0my_script.bat"
set "startup_folder=%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup"
copy "%script_path%" "%startup_folder%\" >nul
if %errorlevel% equ 0 (
echo [OK] Script copied to Startup folder.
) else (
echo [ERROR] Failed to copy script.
)
pause
Using %~dp0 resolves to the directory where the currently running script is located. This ensures you always reference the correct file, regardless of the working directory.
Creating a Proper Shortcut
If you need a .lnk shortcut (for example, to set a working directory or run minimized), you can generate a helper VBScript inline:
@echo off
set "target=%~dp0my_script.bat"
set "shortcut=%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup\MyScript.lnk"
> "%temp%\mklink.vbs" echo Set ws = CreateObject("WScript.Shell")
>> "%temp%\mklink.vbs" echo Set sc = ws.CreateShortcut("%shortcut%")
>> "%temp%\mklink.vbs" echo sc.TargetPath = "%target%"
>> "%temp%\mklink.vbs" echo sc.WindowStyle = 7
>> "%temp%\mklink.vbs" echo sc.Save
cscript /nologo "%temp%\mklink.vbs"
del "%temp%\mklink.vbs"
echo [OK] Shortcut created in Startup folder.
pause
sc.WindowStyle = 7 runs the script minimized. Other values: 1 = Normal, 3 = Maximized.
Method 2: The Windows Registry
The registry provides a more "hidden" way to register startup items. Programs registered here do not appear as files in the Startup folder.
Registry Keys for Startup
- Current User:
HKCU\Software\Microsoft\Windows\CurrentVersion\Run - All Users:
HKLM\Software\Microsoft\Windows\CurrentVersion\Run
Modifying HKLM (all users) requires administrator privileges. HKCU (current user) does not.
Adding a Startup Entry
@echo off
set "script_path=%~dp0my_script.bat"
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v "MyStartupScript" /t REG_SZ /d "\"%script_path%\"" /f
if %errorlevel% equ 0 (
echo [OK] Script registered in registry for current user.
) else (
echo [ERROR] Failed to register.
)
pause
Common Mistake: Unquoted Paths
:: WRONG - path with spaces will break
REG ADD "HKCU\...\Run" /v "MyScript" /d "C:\My Scripts\run.bat" /f
Output Concern:
Windows will try to execute C:\My as the program and Scripts\run.bat as an argument, causing the startup entry to fail silently.
The Correct Way: Properly Escaped Quotes
REG ADD "HKCU\...\Run" /v "MyScript" /d "\"C:\My Scripts\run.bat\"" /f
The escaped inner quotes ensure the full path is treated as a single entity.
Removing a Startup Entry
@echo off
REG DELETE "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v "MyStartupScript" /f
echo [OK] Startup entry removed.
pause
Method 3: Task Scheduler
Task Scheduler is the most powerful option. It allows you to define triggers (at logon, at a specific time, on an event), run with elevated privileges, and configure retry behavior.
Creating a Scheduled Task via Batch
The schtasks command creates and manages scheduled tasks from the command line.
@echo off
set "script_path=%~dp0my_script.bat"
schtasks /create /tn "MyStartupTask" /tr "\"%script_path%\"" /sc onlogon /rl highest /f
if %errorlevel% equ 0 (
echo [OK] Scheduled task created.
) else (
echo [ERROR] Failed to create task.
)
pause
Parameter Breakdown
| Parameter | Description |
|---|---|
/tn | Task name (must be unique) |
/tr | Task to run (the script path) |
/sc onlogon | Trigger: runs at user logon |
/rl highest | Run level: highest privileges (admin) |
/f | Force overwrite if task already exists |
Listing and Removing Scheduled Tasks
:: List all tasks with "MyStartup" in the name
schtasks /query /tn "MyStartupTask"
:: Remove the task
schtasks /delete /tn "MyStartupTask" /f
Task Scheduler is the recommended method for production environments because it provides logging, error handling, and the ability to run tasks even if no user is logged on.
Complete Startup Manager Script
This all-in-one script lets the user choose which method to use for registering or removing startup entries.
@echo off
title Startup Registration Manager
setlocal enabledelayedexpansion
:: The script to be registered (change this to your target script)
set "script_path=%~dp0my_script.bat"
:: Extract filename from path for consistency
for %%F in ("%script_path%") do set "script_name=%%~nxF"
set "startup_folder=%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup"
set "reg_key=HKCU\Software\Microsoft\Windows\CurrentVersion\Run"
set "reg_value=MyBatchScript"
set "task_name=MyBatchStartup"
:menu
cls
color 0F
echo =============================================
echo STARTUP REGISTRATION MANAGER
echo =============================================
echo.
echo Target Script Path: %script_path%
echo Target Filename: !script_name!
echo.
echo --- Register ---
echo [1] Copy to Startup Folder (Current User)
echo [2] Add to Registry (Current User)
echo [3] Create Scheduled Task (Requires Admin)
echo.
echo --- Remove ---
echo [4] Remove from Startup Folder
echo [5] Remove from Registry
echo [6] Delete Scheduled Task
echo.
echo [7] Check Current Status
echo [8] Exit
echo.
set "opt="
set /p "opt=Select: "
if "!opt!"=="1" (
if not exist "%script_path%" (
echo [ERROR] Source script not found: %script_path%
) else (
copy "%script_path%" "!startup_folder!\" >nul 2>&1
if !errorlevel! equ 0 (
echo [OK] Copied to Startup folder.
) else (
echo [ERROR] Failed to copy. Check permissions.
)
)
pause
goto menu
)
if "!opt!"=="2" (
REG ADD "!reg_key!" /v "!reg_value!" /t REG_SZ /d "\"%script_path%\"" /f >nul
if !errorlevel! equ 0 (
echo [OK] Added to registry.
) else (
echo [ERROR] Failed to add registry entry.
)
pause
goto menu
)
if "!opt!"=="3" (
net session >nul 2>&1
if !errorlevel! neq 0 (
echo [ERROR] Admin rights required for Task Scheduler.
pause
goto menu
)
schtasks /create /tn "!task_name!" /tr "\"%script_path%\"" /sc onlogon /rl highest /f >nul
if !errorlevel! equ 0 (
echo [OK] Scheduled task created.
) else (
echo [ERROR] Failed to create task.
)
pause
goto menu
)
if "!opt!"=="4" (
if exist "!startup_folder!\!script_name!" (
del "!startup_folder!\!script_name!" 2>nul
if !errorlevel! equ 0 (
echo [OK] Removed from Startup folder.
) else (
echo [ERROR] Failed to delete "!script_name!" from Startup folder.
)
) else (
echo [INFO] "!script_name!" was not found in the Startup folder.
)
pause
goto menu
)
if "!opt!"=="5" (
REG QUERY "!reg_key!" /v "!reg_value!" >nul 2>&1
if !errorlevel! equ 0 (
REG DELETE "!reg_key!" /v "!reg_value!" /f >nul 2>&1
if !errorlevel! equ 0 (
echo [OK] Removed from registry.
) else (
echo [ERROR] Failed to delete registry entry.
)
) else (
echo [INFO] Registry entry "!reg_value!" not found.
)
pause
goto menu
)
if "!opt!"=="6" (
schtasks /query /tn "!task_name!" >nul 2>&1
if !errorlevel! equ 0 (
schtasks /delete /tn "!task_name!" /f >nul 2>&1
if !errorlevel! equ 0 (
echo [OK] Scheduled task deleted.
) else (
echo [ERROR] Failed to delete scheduled task.
)
) else (
echo [INFO] Scheduled task "!task_name!" not found.
)
pause
goto menu
)
if "!opt!"=="7" (
echo.
echo --- Current Startup Status ---
echo.
echo [Startup Folder]
if exist "!startup_folder!\!script_name!" (
echo STATUS: FOUND ("!script_name!" exists^)
) else (
echo STATUS: NOT FOUND
)
echo.
echo [Registry Run Key]
REG QUERY "!reg_key!" /v "!reg_value!" >nul 2>&1
if !errorlevel! equ 0 (
echo STATUS: FOUND (Key: !reg_value!^)
) else (
echo STATUS: NOT FOUND
)
echo.
echo [Task Scheduler]
schtasks /query /tn "!task_name!" >nul 2>&1
if !errorlevel! equ 0 (
echo STATUS: FOUND (Task: !task_name!^)
) else (
echo STATUS: NOT FOUND
)
echo.
pause
goto menu
)
if "!opt!"=="8" exit /b
goto menu
Best Practices
- Choose the right method: Use the Startup folder for simplicity, the registry for stealth, and Task Scheduler for robustness.
- Always provide removal: Any script that registers a startup item should also offer a way to unregister it.
- Quote all paths: Paths with spaces will silently fail without proper quoting.
- Test before deploying: Run your startup script manually first to confirm it works before registering it.
- Minimize window: Consider running startup scripts in a minimized window to avoid disrupting the user.
Conclusion
Registering a Batch Script as a startup item is a versatile skill that unlocks a wide range of automation possibilities. Whether you use the Startup folder, the Windows Registry, or Task Scheduler, each method provides a reliable way to ensure your scripts run every time Windows boots. The choice depends on your specific needs for visibility, control, and administrative access.