# Disable UAC via cmd line in batch file



## Deviate

I have a batch file I run to set some basic settings on newly imaged Win 7 laptops. I have the following command running first to disable UAC.

_C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f_

It works fine. However, after the command is run it tells me successfully completed, but it does not proceed to the next command in the batch, unless I type "exit". Upon typing exit, it proceeds with the remaining lines and finishes out the rest of the batch correctly.

I've tried putting an "exit" after the command in the file but that doesn't work. I'm assuming it's something weird since the command is actually making a change to the registry, rather than a typical command line config value change.

Any trick to getting this to proceed without having to type exit after it runs?


Thanks,

Matt


----------



## niemiro

Hello Matt!

I see the problem! However, let me explain it to you, and others who may come by.

reg.exe is a Console Application. It cannot be run by itself, it needs to run under a host, the most common host being cmd.exe.

If you run reg.exe from within a host, ie by running reg.exe from Command Prompt, that host will be used. If you try to start it standalone, a host will open. This you all know already, I am sure.

When running a batch file, a host opens, runs commands from that host, and eventually closes, closing the host.

However, your command specifies that reg.exe must run under a new, second host. You run a second copy of cmd.exe, and run reg.exe.

However, this second host will not close by itself, and the first host will not continue until the second is closed. You putting in "exit" clsoes the second host, and lets the first continue. Putting "exit" in your batch file closes your first host, stopping your script! Not ideal!

So, how can you sort that. You could ask the second host to close (/c) when it has finished your reg.exe command, or remain open (/o). You want it to automatically close.

So:



Code:


C:\Windows\System32\cmd.exe /k /c %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f

will automate your manual "exit" and your script will run nicely.

But opening a Command Prompt declares a lot of variables, and takes a lot of effort. One modern computers this may seem minimal - it opens instantaneously. But efficiency does matter. Use Process Monitor if you want on your second host. That is a lot of work. 

So why open up this second host? There is no point. All you are doing is straining your computer (slightly) and slowing down your script. Let reg.exe execute from your already open host, the one started by the batch file.

So


Code:


%windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f

Would be far better and more efficient.

The only time you should specific the host, and in this situation always call close (/c) is if you are trying to run an internal command from an external source with no host already open.

For example, dir is an internal command. It only works inside cmd.exe, and there is no dir.exe. So if you wanted to run dir from within a program, and not a batch file, 



Code:


C:\Windows\System32\cmd.exe /c dir C:\ > C:\test.txt

There are problems with this. Why would you want to use dir like this anyway. That is not the point.

I hope this helps you to understand!

Richard


----------



## Deviate

Richard,

Thank you! For some reason I was overlooking the fact that I was actually opening up another instance of cmd.exe. That deserves a facepalm.

I did as you did and just omitted the opening of the second host process, and it works like a charm. I will also keep in mind the /c switch for closing out a house for future reference, should I actually have to open one from within another.

Thanks again for the help!


----------



## niemiro

Hello Matt!

It was no problem at all! Really glad to have been of assistance, and I hope that you learned possibly something as well as just achieving the result!

Sometimes no matter how hard you look, you just can't see the bug. I have asked quite a few questions of people who know one load of a lot more than I, sometimes with a "facepalm", and sometimes with a "If you say so ... it works!!!!"

Richard


----------

