[Bat] How to automatically promote to administrator authority if the batch is not executed with administrator authority

Windows

Hello!This time, as a summary up to the last time, let's create a batch that executes itself as an administrator when it is not executed with administrator privileges.

Normally, when starting a batch file, it is often executed by double-clicking.
However, in that case, the process may fail because you do not have administrator privileges.

In such a case, I'm grateful that it will be automatically executed with administrator privileges.

This time, I will introduce how to create a batch file that automatically promotes to administrator privileges regardless of such startup method.

Make sure you have administrator privileges

First, let's review the part to check if you have administrator privileges.
You can read more about it in the article I wrote earlier.

This time I will check it using Whoami.

whoami / priv | find "SeDebugPrivilege"> nul if% errorlevel% neq 0 (
 echo Not administrator privileges
)

echo Administrator privileges
pause
exit

Run with administrator privileges

This is also quoted from the previous article.

This time, it will be executed with administrator privileges by the method using PowerShell.

powershell start-process cmd -verb runas

Run as administrator if you do not have administrator privileges

If you use the above two functions and combine them into one, it will be automatically promoted to administrator authority even if you unintentionally execute it with general authority, such as when you open it with one double click.

whoami / priv | find "SeDebugPrivilege"> nul
if% errorlevel% neq 0 (

 @powershell start-process "% ~ 0 " -verb runas
 echo You do not have administrator privileges.Run with administrator privileges
 exit
)
echo I was able to automatically promote to administrator privileges.
pause
exit

%-0Is a variable that returns its own file path

In this way, we have created a batch that automatically promotes to administrator privileges.
If you double-click it to open it, it will automatically reopen and you can see that it can be executed with administrator privileges.

I hope it helps you.

Comment

  1. […] How to automatically promote batch to administrator privileges Correct-Log — Collelog — […]

Translate »
I copied the title and URL