4

OS: Win 7 I googled a lot and I found one answer here which not helping me as it outputs just the answer (I cannot used it in IF statement

I have also found this

if  /i  "%SAFEBOOT_OPTION%"=="MINIMAL" echo We're in Safe Mode! 

I have tried it almost 10 times and the SAFEBOOT_OPTION variable is always empty.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
user3058737
  • 45
  • 1
  • 4

4 Answers4

3

The %SAFEBOOT_OPTION% variable only exists if you are currently booted into safe mode (contains MINIMAL) or safe mode with networking (contains NETWORK).

From Microsoft's Docs:

An environment variable is set when you use one of the Safe Boot options. The environment variable is SAFEBOOT_OPTION. This variable is set to either Network or to Minimal.

EDIT

Just tested the script below while running in safe mode:

@echo off
if /i "%SAFEBOOT_OPTION%"=="MINIMAL" echo We're in Safe Mode!

This script printed We're in Safe Mode! to cmd as expected.

Taylor Hx
  • 2,815
  • 23
  • 36
  • 2
    Two minor problems here. First, as you say, ** This variable is set to either Network or to Minimal.** which means that the test should be `if defined safeboot_option echo We're in safe mode! (%safeboot_option%)` Second problem is that if `safeboot_option` is **NOT** set, your posted statement would generate a syntax error - you'd need to `"quote"` the operands (or otherwise ensure they're not empty such as `...Q%SAFEBOOT_OPTION%==QMINIMAL...` – Magoo Dec 16 '13 at 05:40
  • @Magoo You make an excellent point about quoting the operands (which I neglected to do). This has been rectified with an edit. This also resolves the case where `%SAFEBOOT_OPTION%` is undefined, as it will now evaluate to `""`, which will not match `MINIMAL`. This could be changed to `if defined`, if you wanted to detect both `safe mode` and `safe mode with networking`. I used a comparison with `MINIMAL` to more closely resemble the code posted by user3058737. – Taylor Hx Dec 16 '13 at 05:48
0

If you have a command which outputs the mode, then you can use it in a IF.

For eg. the command is MyCommand which outputs "You are in safe mode" if you booted in safe mode, then you can use the following

@echo off
MyCommand | findstr /c:"You are in safe mode" > nul
if errorlevel 1 goto NotSafe

echo Safe
REM Do whatever you want in Safe Mode
goto end

:NotSafe
echo NotSafe
REM Do whatever you want in Safe Mode
:end

If you want to check for a single word instead of a space separated string you do not need the /c: in findstr. You can just use findstr SafeMode.

user93353
  • 13,733
  • 8
  • 60
  • 122
0

Hey Everyone I know the thread is a bit old. However I recently ran into an issue with a batch i did not want technicians to run in safe mode. However I wasnt really able to find an answer. Using the above however I was able to peruse the rest of the web and finally came up with a check that can be done when you want other work done as well. So in this case if it detects safe mode it will goto :safemode and quit. or continue the job.

wmic COMPUTERSYSTEM GET BootupState | findstr /i "fail-safe" > nul
if %errorlevel% EQU 0 goto safemode

//stuff

:safemode
    cls
    Echo.
    Echo Halting system is in SAFEMODE!!
Solaris17
  • 73
  • 1
  • 1
  • 5
0

If you just want to detect if the system is in normal mode or safe mode, try this using batch program, try this simple:

@echo off

if "%SAFEBOOT_OPTION%"=="" goto normal

echo This computer is in safe mode!

goto ext

:normal

echo This computer is running in normal mode!

goto ext

:ext

pause

exit

If the SAFEBOOT_OPTION variable is empty, then the system is in normal mode.

Community
  • 1
  • 1
Nathan
  • 1