2

can a bat file or vbs file take an argument (a path to an exe file) then create a shortcut to that file and place in the startup folder or add a registry entry to run that exe at windows startup

but i want the same script to be able to remove the startup entry aswell (on all OS version).

so i assume the script would need to be sent two parameters/arguments: 1 - the file/exe to run at startup 2 - whether to add or remove the entry from startup

Is this possible?

  • Yes, it's possible. Look up how to pass arguments into a batch or VBS file, they both have support for it, then it's simply adding IF statements to handle them. – Jonno Feb 2 '16 at 5:05
2

First I would like to advise you to double check the run key locations in the registry for any operating system older than vista, as it has been a long time since I worked on them, so there may be differences that I don't remember.

If I"m understanding your question correctly this batch script should do what you want. It gives you a simple menu to choose to add or remove a program from the run keys(startup), and then lets you put in the program name and path to the executable.

:begin
cls
@echo off
echo   Program startup Utility
echo.
echo    1. Add Program to Startup
echo    2. Remove Program From Startup
echo    x. Exit

set /p choice=  Choose A Service:
if not '%choice%'== set %choice%=choice:~0,1%

if '%choice%'=='1' goto :addstartup
if '%choice%'=='2' goto :delstartup
if '%choice%'=='x' goto :exit

:addstartup
cls
echo/
echo/
echo    Add Program to Startup
echo    or type back to go to main menu
echo/
set /p keyname= Please State Program Name:

if '%keyname%'=='back' goto :begin

set /p expath= Please Enter Path to Executible:

if '%expath%'=='back' goto :begin

reg add HKLM\Software\Microsoft\Windows\CurrentVersion\Run\ /v %keyname% /t REG_SZ /d "%expath%"

timeout /t 3 >nul

goto begin


:delstartup
cls
echo/
echo  Remove Program From Startup
reg query HKLM\Software\Microsoft\Windows\CurrentVersion\Run\ /s
echo/
set /p keyname= Please Enter Program Name:
reg delete HKLM\Software\Microsoft\Windows\CurrentVersion\Run\ /v "%keyname%" /f

timeout /t 3 >nul

goto begin

Alternatively if you just need to be able to add or remove the same program and don't need to enter in multiple program names or exe paths, then you can modify the script like this, and replace %keyname% with your program name, and %expath% with the path to your executable(without the %'s) and then save it. Make sure your keyname is the same on both add and remove.

You can also exchange the "goto begin" lines with "exit" if you just want to quit the script after the selected action.

:begin
cls
@echo off
echo   Program startup Utility
echo.
echo    1. Add Program to Startup
echo    2. Remove Program From Startup
echo    x. Exit

set /p choice=  Choose A Service:
if not '%choice%'== set %choice%=choice:~0,1%

if '%choice%'=='1' goto :addstartup
if '%choice%'=='2' goto :delstartup
if '%choice%'=='x' goto :exit

:addstartup
cls
reg add HKLM\Software\Microsoft\Windows\CurrentVersion\Run\ /v %keyname% /t REG_SZ /d "%expath%"

timeout /t 2 >nul

goto begin

:delstartup
cls
reg delete HKLM\Software\Microsoft\Windows\CurrentVersion\Run\ /v "%keyname%" /f

timeout /t 2 >nul

goto begin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.