Skip to content

Instantly share code, notes, and snippets.

@theorium-0
Created July 12, 2025 00:23
Show Gist options
  • Save theorium-0/c02602a0802f505de43ababa99d7611c to your computer and use it in GitHub Desktop.
Save theorium-0/c02602a0802f505de43ababa99d7611c to your computer and use it in GitHub Desktop.
Simply requests admin privileges.
@echo off
setlocal enableextensions enabledelayedexpansion
cls
:: Check for administrative privileges
NET SESSION >NUL 2>&1
IF %ERRORLEVEL% NEQ 0 (
echo Requesting administrative privileges
goto :elevate
) ELSE (
echo This script is running as "%COMPUTERNAME%\%USERNAME%"
)
:elevate
:: Embed and execute the VBScript directly using cscript/wscript
:: The batch file will be executed as a VBScript, and then that VBScript
:: will launch the batch file again as administrator.
:: Execute the batch file itself as a VBScript, telling it to elevate
cscript //nologo "%~f0" "VBSCRIPT_ELEVATE"
exit /b
:: --- VBScript Block (must be at the end of the batch file) ---
<nul>
@goto :EOF
'
' This is the VBScript portion.
' It will only be executed when the batch file is run by cscript/wscript
' (i.e., during the elevation step).
'
If WScript.Arguments.Count > 0 Then
If WScript.Arguments(0) = "VBSCRIPT_ELEVATE" Then
' Get the path to the current batch file
Dim objShell, strScriptPath
Set objShell = CreateObject("Shell.Application")
strScriptPath = WScript.ScriptFullName
' Run the batch file again as administrator
' The "" at the end ensures no arguments are passed to the elevated instance,
' so it doesn't get stuck in an endless elevation loop.
objShell.ShellExecute strScriptPath, "", "", "runas", 1
WScript.Quit
End If
End If
' End of VBScript block
@theorium-0
Copy link
Author

@theorium-0
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment