Skip to content

Instantly share code, notes, and snippets.

@Clijsters
Last active January 6, 2023 12:59
Show Gist options
  • Save Clijsters/e8460c8f4614553bba21 to your computer and use it in GitHub Desktop.
Save Clijsters/e8460c8f4614553bba21 to your computer and use it in GitHub Desktop.
Sudo for Windows - Self elevating .bat-File
@echo off
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' (
goto mainScript
) else (
goto getPrivileges
)
::-------------------------------------------------------------------------------------------------
:getPrivileges
if '%1'=='ELEV' (shift & goto mainScript)
echo.
echo Selbstausfuehrung mit Administratorrechten...
setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\runAsAdmin.vbs"
echo UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\runAsAdmin.vbs"
"%temp%\runAsAdmin.vbs"
exit /B
::-------------------------------------------------------------------------------------------------
:mainScript
REM Here we are doing admin stuff...
cls
echo Hallo Welt >C:\test.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment