It doesn't contain any double quotes, so it is safe to say "!batchPath!" later in the script. You can then access the path by using !batchPath!. The double quotes are removed by the following (details are here): setlocal DisableDelayedExpansion I have fixed this now by stripping the quotes in the path and re-adding them later, and I've added an extra parameter which is added when the script re-launches with elevated rights. One was due to different double quote handling, and the other issue was due to the fact that UAC was disabled (set to lowest level) on a Windows 7 machine, hence the script calls itself again and again. On some machines, I've encountered issues, which are solved in the new version above already. It is displayed here in multiple lines for better readability! Important: NET FILE EXIT /D) must be on the same line. If you don't require the PAUSE, just remove it. The script will proceed after the REM statement if it detects administrator rights, otherwise exit with an error. This way, the user has to right-click and select "Run as administrator". NET FILE 1>NUL 2>NUL & IF ERRORLEVEL 1 (ECHO You must right-click and select &ĮCHO "RUN AS ADMINISTRATOR" to run this batch. If your script just needs to show an error message and exit if there aren't any administrator privileges instead of auto-elevating, this is even simpler: You can achieve this by adding the following at the beginning of your script: OFF & CLS & ECHO.
Without this privilege elevating script, UAC would ask you three times for your administrator user and password - now you're asked only once at the beginning, and only if required.
The advantage is, after the start point you can place anything that requires system administrator privileges, for example, if you intend to re-install and re-run a Windows service for debugging purposes (assumed that mypackage.msi is a service installer package): msiexec /passive /x mypackage.msi I have tested it with Windows 7, 8, 8.1, 10 and with Windows XP - it works fine for all.
This causes Windows to present the UAC dialog and asks you for the administrator account and password. The elevation is achieved by creating a script which re-launches the batch file to obtain privileges. The script takes advantage of the fact that NET FILE requires administrator privilege and returns errorlevel 1 if you don't have it.
REM Run shell as admin (example) - put here code as you likeĮCHO %batchName% Arguments: P1=%1 P2=%2 P3=%3 P4=%4 P5=%5 P6=%6 P7=%7 P8=%8 P9=%9 If '%1'='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul & shift /1) "%SystemRoot%\%winSysFolder%\WScript.exe" "%vbsGetPrivileges%" %* If '%1'='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)ĮCHO **************************************ĮCHO Invoking UAC for Privilege EscalationĮCHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"ĮCHO args = "ELEV " > "%vbsGetPrivileges%"ĮCHO For Each strArg in WScript.Arguments > "%vbsGetPrivileges%"ĮCHO args = args ^& strArg ^& " " > "%vbsGetPrivileges%"ĮCHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 > "%vbsGetPrivileges%"ĮCHO args = "/c """ + "!batchPath!" + """ " + args > "%vbsGetPrivileges%"ĮCHO UAC.ShellExecute "%SystemRoot%\%winSysFolder%\cmd.exe", args, "", "runas", 1 > "%vbsGetPrivileges%" If '%errorlevel%' = '0' ( goto gotPrivileges ) else ( goto getPrivileges ) Set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs" :: Automatically check & get admin rights There is an easy way without the need to use an external tool - it runs fine with Windows 7, 8, 8.1 and 10 and is backwards-compatible too (Windows XP doesn't have any UAC, thus elevation is not needed - in that case the script just proceeds).Ĭheck out this code (I was inspired by the code by NIronwolf posted in the thread Batch File - "Access Denied" On Windows 7?), but I've improved it - in my version there isn't any directory created and removed to check for administrator privileges): ::::::::::::::::::::::::::::::::::::::::::::