Reputation: 69
How to script a batch file that would run a check whether or not UAC is enabled by:
REG QUERY HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\ /v EnableLUA
It is enabled if the result is:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System EnableLUA REG_DWORD **0x1**)
and it is disabled if the result is:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System EnableLUA REG_DWORD **0x0**)
The script should then run different commands according to the result.
Upvotes: 5
Views: 7798
Reputation: 851
Check out the Microsoft documentation about EnableLUA
here:
@echo off
for /f "skip=2 tokens=3" %%a in ('reg query HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA') do if "%%a" equ "0x0" (
rem When UAC is disabled
) ELSE (
rem When UAC is enabled
)
Upvotes: 1
Reputation: 77707
You could simply search for one or the other value with either FIND
or FINDSTR
and invoke commands depending on the result of the search. The pattern would basically be like this:
REG QUERY HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\ /v EnableLUA | (
FIND "0x1" >NUL
) && (
command(s)_to_run_when_UAC_is_enabled
) || (
command(s)_to_run_when_UAC_is_disabled
)
I.e. the REG
produces an output which is passed, using the "pipe" (|
) to the input of FIND
. FIND
looks for 0x1
in its input, and, depending on the result of the search, one of the following bracketed blocks of commands is executed.
The command && command || command
is a standard mechanism that allows you to selectively run commands, a kind of replacement for IF
. The first command produces a result. The command just after &&
runs if the result is "success", and the command just after ||
runs in case of a fail.
If you need to perform actions in both cases, use both &&
and ||
after the command generating the result, but if only one kind of result should be reacted to, you can leave out either &&
or ||
.
Upvotes: 2