سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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.

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!!

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top