Restart MS Windows in Safe Mode with batch file

Sep 09, 2012 09:01

The problem with forcing "Safe Mode" through "msconfig" is that if the "Safe Mode" settings are missing or corrupted then the computer ends up in an endless reboot cycle without the chance to boot into Windows normaly. Thus forcing the computer into "Safe Mode" is best avoided if you can still otherwise get into Window in Normal Mode and edit the boot manager.

So I made a batch script that does the work of adding a Safe Mode option to the boot manager. It may also help those with various wireless keyboards that otherwise cannot be used to get into Safe Mode.

I hope it helps some. I've tested it with Windows 7 and Windows XP. It is designed to remove it's settings afterwards.

You do need to turn off any security software because it may suspect my work is a trojan even though it is clearly not.

Here is a pre-made version... http://ares.webcity.com.au/~siz47881/misc/kaspbats/SafeMode/SafeModeBoot.bat

and here is the script...



@echo off

:: Get ADMIN Privs

:-------------------------------------

echo.

echo. Safe Mode ON/OFF v12.9.9

echo.

echo. Written by Justin Godden 2012

echo.

echo. email: the_jagaroth@yahoo.com.au

echo.

mkdir "%windir%\BatchGotAdmin"

if '%errorlevel%' == '0' (

rmdir "%windir%\BatchGotAdmin" & goto gotAdmin

) else ( goto UACPrompt )

:UACPrompt

echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"

echo UAC.ShellExecute %0, "", "", "runas", 1 >> "%temp%\getadmin.vbs"

"%temp%\getadmin.vbs"

exit /B

:gotAdmin

if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )

pushd "%CD%"

CD /D "%~dp0"

:-------------------------------------

:: End Get ADMIN Privs

REM Changing working folder back to current directory

%~d0

CD %~dp0

REM Folder changed

if exist "%AllUsersProfile%\boot.iniSMba" goto SFXP_detected

echo.test1

REM Check first if Windows XP

for /f "tokens=3*" %%i IN ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName ^| Find "ProductName"') DO set vers=%%i %%j

echo %vers% | find "XP" > nul

if %ERRORLEVEL% == 0 goto ver_xp

if exist "%AllUsersProfile%"\bcdoutput.txt goto already_set

if exist "%AllUsersProfile%"\bcdoutput2.txt goto already_set

bcdedit /copy {current} /d "Safe Mode (with Networking)" > "%AllUsersProfile%"\bcdoutput.txt

set /p avp_setup2012a= < "%AllUsersProfile%"\bcdoutput.txt

REM strip spaces from end of file name...

set avp_setup2012a=%avp_setup2012a%##

set avp_setup2012a=%avp_setup2012a: ##=##%

set avp_setup2012a=%avp_setup2012a: ##=##%

set avp_setup2012a=%avp_setup2012a: ##=##%

set avp_setup2012a=%avp_setup2012a: ##=##%

set avp_setup2012a=%avp_setup2012a: ##=##%

set avp_setup2012a=%avp_setup2012a:##=%

if "%avp_setup2012a%" == "" goto hi0bokx

ECHO.

echo Located installer: %avp_setup2012a%

echo %avp_setup2012a:~37,38% > "%AllUsersProfile%"\bcdoutput2.txt

set /p bcd_id= < "%AllUsersProfile%"\bcdoutput2.txt

echo.%bcd_id%

bcdedit /set %bcd_id% safeboot network

:safeadded

copy /Y %0 "%AllUsersProfile%\SafeModeBoot.bat"

REG ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v SafeModeBoot /t REG_SZ /d "%AllUsersProfile%\SafeModeBoot.bat" /f

cls

echo.

echo. "Safe Mode" option added.

echo.

echo. You must restart your computer

echo.

echo. Then choose "Safe Mode (with Networking)" when asked.

echo.

:hi0bokx

ECHO MsgBox Chr(13)+"''Safe Mode'' option now added on restart."+Chr(13)+Chr(13)+Chr(13)+"Click on 'OK' to restart computer."+Chr(13)+Chr(13)+"Then choose ''Safe Mode (with Networking)'' when asked."+Chr(13)+Chr(13) > "%Temp%"\endalert.vbs

ECHO.

cscript //nologo "%Temp%"\endalert.vbs

REM PAUSE

DEL /Q "%Temp%"\endalert.vbs

shutdown /r /t 0

goto end_it

:already_set

set /p bcd_id= < "%AllUsersProfile%"\bcdoutput2.txt

if "%bcd_id%" == "" goto hi0bokx

echo.%bcd_id%

bcdedit /delete %bcd_id%

:saferemoved

REG DELETE HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v SafeModeBoot /f

cls

echo.

echo. Safe Mode option removed.

echo.

del /F /Q "%AllUsersProfile%"\bcdoutput.txt

del /F /Q "%AllUsersProfile%"\bcdoutput2.txt

goto :end_it

:ver_xp

attrib "%systemdrive%"\boot.ini -s -r -h

copy /Y "%systemdrive%"\boot.ini "%AllUsersProfile%"\boot.iniSMba"

Find "default=" C:\boot.ini > default

For /f "skip=2 tokens=2,3* delims== " %%i IN (default) DO @set default=%%i

rem For /F "tokens=5,6* skip=4 delims=: " %%i IN (C:\boot.ini) DO @set mode=%%k

echo>> "%systemdrive%"\boot.ini %default%="Safe Mode (with Networking)" /noexecute=alwaysoff /fastdetect /safeboot:Network

Echo. Your PC is now set to start in Safe Mode.

attrib "%systemdrive%"\boot.ini +h +r +s

goto safeadded

:SFXP_Detected

attrib "%systemdrive%"\boot.ini -s -r -h

move /Y "%systemdrive%\boot.ini" "%AllUsersProfile%\boot.inibax"

del /Q /F "%systemdrive%"\boot.ini"

copy /Y "%AllUsersProfile%\boot.iniSMba" "%systemdrive%\boot.ini"

move /Y "%AllUsersProfile%\boot.iniSMba" "%AllUsersProfile%\boot.iniSMbax"

attrib "%systemdrive%"\boot.ini +h +r +s

goto saferemoved

:end_it

Previous post Next post
Up