New
#11
To add to your confusion about "Run as administrator":
(Net session >nul 2>&1)||(PowerShell start """%~0""" -verb RunAs & Exit /B)
To add to your confusion about "Run as administrator":
(Net session >nul 2>&1)||(PowerShell start """%~0""" -verb RunAs & Exit /B)
Thanks @Kyhi, and @Matthew Wai,
When I initially looked into finding a solution to check if a batch file was running with Admin privileges, there were many variations available. Some of the examples were . . .
Code:@echo off NET SESSION >NUL 2>&1 IF %ERRORLEVEL% EQU 0 ( echo Admin ) ELSE ( echo NOT Admin ) Pause >NULCode:@echo off Goto Check_Admin :Check_Admin Net Session >NUL 2>&1 If %ERRORLEVEL% == 0 ( echo Admin ) else ( echo NOT Admin ) Pause >NULCode:@echo off Call :IsAdmin If %ERRORLEVEL% == 0 ( echo Admin ) ELSE ( echo Not Admin ) Pause >NUL Exit /b :IsAdmin fsutil dirty query %systemDrive% >NUL Exit /bCode:@echo off fltmc >NUL 2>&1 && ( echo Admin ) || ( echo NOT Admin ) Pause >NUL
This one is for 64-bit only which I got from here => Rob van der Woude's Scripting Pages.
Code:@echo off OPENFILES >NUL 2>&1 IF ERRORLEVEL 1 ( ECHO This batch file requires elevated privileges EXIT /B 1 ) Pause >NUL
I assume that BOTH your codes @Kyhi and @Matthew Wai run on 32-bit and 64-bit systems?
Are you saying that the code I used is not valid?
I have used this in other scripts and I have never had an issue from people.
@Matthew Wai, does your code do exactly the same as @Kyhi's code?
I am in lockdown at my Sister's and I don't have access to a Win 10 computer during the day for testing purposes!
Last edited by Paul Black; 20 May 2020 at 04:35.
Reset Windows Update in Windows 10
Previously, I used the above code, which has been added into Brink's script there.
I don't use it anymore because it is long and complicated. The code in my post #11 here is simple and works fine. Both will get administrative privileges automatically. You don't have to right-click and select "Run as administrator".
Hello @Matthew Wai,
I understand that. When I write a brief description of what each part of the Menu does I was going to state that the .bat file needs to be Run as administrator, hence the prompt in my code if they forgot to do that!
I will try them out later if I can get access to a Win 10 computer!Try out both and see whether there is a difference.
As it is a .bat file then, will your code need to be changed to cmd.exe start?
PowerShell and VBScript are used in the former and the latter respectively. That's the difference.
No change is needed. Try the following in a .bat file:
It will say "Hello".Code:@echo off (Net session >nul 2>&1)||(PowerShell start """%~0""" -verb RunAs & Exit /B) Echo Hello & pause
Last edited by Matthew Wai; 20 May 2020 at 08:07.
OK Matt, you are good with codes...
My vbs method sets Admin when running .cmd file directly... (Paul - it work with every OS)
My problem is it does not set Admin rights if I drop a file on it to open
If the %params% are present - the command fails with vbs error - I believe it has something to do with the quotes.
But it may also have to do with MS doing away with vbs - as it use to work with out issue by dropping a file on cmd..
Not sure when it all changed - but it appears to have effected all my batch files..
Paul My Script
Code:@echo off title IMAGE HEALTH v20.05.19 rem ================ YY.MM.DD color 17 set "params=%*" setlocal EnableDelayedExpansion cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || ( echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit /B ) %windir%\system32\reg.exe query "HKU\S-1-5-19" 1>nul 2>nul || ( echo ============================================================ echo ERROR: Run The Script As Administrator. echo ============================================================ echo. echo. echo Press any key to exit... pause >nul goto :eof ) if exist "%windir%\logs\cbs\cbs.log" (del /f /q "%windir%\logs\cbs\cbs.log" >nul 2>&1) if exist "%windir%\logs\dism\dism.log" (del /f /q "%windir%\logs\dism\dism.log" >nul 2>&1) if exist "%~dp0bin\wimlib-imagex.exe" (set "wimlib=%~dp0bin\wimlib-imagex.exe") IF /I %PROCESSOR_ARCHITECTURE%== x86 (If Not Defined PROCESSOR_ARCHITEW6432 set "dism=%~dp0bin\ADK_6\x86\Dism\dism.exe") else (set "dism=%~dp0bin\ADK_6\amd64\Dism\dism.exe") :dismcheck cls set userinp= echo. echo =============================================================================== echo. Press 0 - To Run DISM Commands Using the Dism Files From Local Host echo. Press 1 - To Run DISM Commands Using Latest Win10 ADK Dism Download echo =============================================================================== set /p userinp= ^> Enter Your Option: if [%userinp%]==[] echo.&echo Invalid User Input&echo.&pause&goto :dismcheck if %userinp% gtr 1 echo.&echo Invalid User Selection&echo.&pause&goto :dismcheck if %userinp%==0 goto :dismhost if %userinp%==1 goto :dismdownload :dismdownload if not exist %dism% call %~dp0bin\GWT.exe -win10dism -Silent if not exist %dism% (goto :dismhost) else ( echo. echo.Using DISM From Windows 10 ADK Download.. echo. goto :menu ) :dismhost set "dism=%systemroot%\system32\dism.exe" echo. echo.Using DISM From Host System Files.. echo. goto :menu :menu title IMAGE HEALTH v20.05.19 rem ================ YY.MM.DD cls set userinp= echo. echo =============================================================================== echo. echo. Press 1 - Run Dism /Online /Cleanup-Image /ScanHealth echo. Press 2 - Run Dism /Online /Cleanup-Image /RestoreHealth echo. Press 3 - Run Dism /Online /Cleanup-Image /RestoreHealth /Source echo. Press 4 - Run Dism /Online /Cleanup-Image /AnalyzeComponentStore echo. Press 5 - Run Dism /Online /Cleanup-Image /StartComponentCleanup echo. Press 6 - Run Dism /Online /Cleanup-Image /StartComponentCleanup /ResetBase echo. Press 7 - Run SFC /Scannow echo. echo. Press 8 - Dism Revert Pending Actions echo. Press 9 - Disk Cleanup Manager ^> Extended Mode echo. Press 10 - Schedule CheckDisk to Scan ^& Fix Disk Errors echo. Press 11 - Retrieve Windows Product Key ^> ShowKeyPlus echo. Press 12 - Export 3rd Party System Drivers echo. Press 13 - Restore User Files from Windows.old echo. Press 14 - Generate a GenuineTicket.xml File echo. Press 15 - Download Media From TechBench echo. Press 16 - Download Preview-Release Media From UUPdump echo. echo. Press 0 - EXIT PROGRAM and CREATE DISM REPORT echo =============================================================================== set /p userinp= ^> Enter Your Option: if [%userinp%]==[] echo.&echo Invalid User Input&echo.&pause&goto :menu if %userinp% gtr 15 echo.&echo Invalid User Selection&echo.&pause&goto :menu if %userinp%==0 goto :done if %userinp%==1 goto :opt1 if %userinp%==2 goto :opt2 if %userinp%==3 goto :opt3 if %userinp%==4 goto :opt4 if %userinp%==5 goto :opt5 if %userinp%==6 goto :opt6 if %userinp%==7 goto :opt7 if %userinp%==8 goto :opt8 if %userinp%==9 goto :opt9 if %userinp%==10 goto :opt10 if %userinp%==11 goto :opt11 if %userinp%==12 goto :opt12 if %userinp%==13 goto :opt13 if %userinp%==14 goto :opt14 if %userinp%==15 goto :opt15 if %userinp%==16 goto :opt16 goto :eof :opt1 cls echo. echo =============================================================================== echo. Dism is scanning the online image for health. echo. Dism will report whether the image is Healthy or Repairable echo. If Dism reports as repairable - Select Option 2 echo. Please Wait.. This will take 5-15 minutes.. echo =============================================================================== if exist "%windir%\logs\cbs\cbs.log" (del /f /q "%windir%\logs\cbs\cbs.log" >nul 2>&1) %Dism% /Online /Cleanup-Image /ScanHealth copy "%windir%\logs\cbs\cbs.log" "%UserProfile%\Desktop\ScanHealth Details.txt" pause goto :menu :opt2 cls echo. echo =============================================================================== echo. Dism uses Windows Update to provide the files required to fix corruption. echo. If Dism reports Source Files Could Not Be Found - Select Option 3 echo. Please Wait.. This will take 10-15 minutes or more.. echo =============================================================================== if exist "%windir%\logs\cbs\cbs.log" (del /f /q "%windir%\logs\cbs\cbs.log" >nul 2>&1) %Dism% /Online /Cleanup-Image /RestoreHealth copy "%windir%\logs\cbs\cbs.log" "%UserProfile%\Desktop\RestoreHealth Details.txt" pause goto :menu :opt3 cls for %%I in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do if exist "%%I:\\setup.exe" set setup=%%I if defined setup (for %%I in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do if exist "%%I:\\autorun.inf" set autorun=%%I) if defined autorun (for %%I in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do if exist "%%I:\\sources\install.wim" set setupdrv=%%I) if defined setupdrv ( echo. echo =============================================================================== echo. Found Source Install.wim on Drive %setupdrv%: echo =============================================================================== goto :wimmenu ) for %%I in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do if exist "%%I:\\setup.exe" set setup=%%I if defined setup (for %%I in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do if exist "%%I:\\autorun.inf" set autorun=%%I) if defined autorun (for %%I in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do if exist "%%I:\\sources\install.esd" set setupdrv=%%I) if defined setupdrv ( echo. echo =============================================================================== echo. Found Source Install.esd on Drive %setupdrv%: echo =============================================================================== goto :esdmenu ) if not defined setupdrv ( echo. echo =============================================================================== echo. No Installation Media Found! echo. echo. Mount Media ISO or Insert DVD or USB flash drive and run this option again. echo =============================================================================== pause goto :menu ) :wimmenu cls set userinp= echo. echo =============================================================================== echo. Press 0 - Use Source^:Wim^:Install.wim as Repair Method ^( source:wim: ^) echo. Press 1 - Mount the Windows Image as the Repair Source ^( Recommended ^) echo =============================================================================== set /p userinp= ^> Enter Your Option: if [%userinp%]==[] echo.&echo Invalid User Input&echo.&pause&goto :wimmenu if %userinp% gtr 1 echo.&echo Invalid User Selection&echo.&pause&goto :wimmenu if %userinp%==0 goto :restorewim if %userinp%==1 goto :mountwim goto :menu :restorewim echo. echo =============================================================================== echo. Dism is restoring the online image health using the source Install.wim echo. Please Wait.. This will take 10-15 minutes or more.. echo =============================================================================== %Dism% /Online /Cleanup-Image /RestoreHealth /source:wim:%setupdrv%:\sources\install.wim:1 /limitaccess pause goto :menu :Mountwim echo. echo =============================================================================== echo. Mounting the Source Install.wim as the Repair Source echo. Please Wait.. This will take 5-10 minutes.. echo =============================================================================== md "%~dp0Source\Mount" copy %setupdrv%:\sources\install.wim "%~dp0Source\install.wim" if not exist "%~dp0Source\install.wim" (echo.&echo ERROR: Required File Copy Failed - File Not Found.&pause&goto :Done) %Dism% /Mount-Image /ImageFile:"%~dp0Source\install.wim" /Index:1 /MountDir:"%~dp0Source\Mount" echo. echo =============================================================================== echo. Dism is restoring the online image health using the mounted source media. echo. Please Wait.. This will take 10-15 minutes or more.. echo =============================================================================== %Dism% /Online /Cleanup-Image /RestoreHealth /Source:"%~dp0Source\Mount\Windows" /LimitAccess %Dism% /Unmount-Image /MountDir:"%~dp0Source\Mount" /discard pause goto :menu :esdmenu cls set userinp= echo. echo =============================================================================== echo. Press 0 - Use the Source^:ESD^:Install.esd as Repair Method ^( soure:esd: ^) echo. Press 1 - Export Install.esd to Install.wim as Repair Source ^( source:wim: ^) echo. Press 2 - Mount the Windows Image as the Repair Source ^( Recommended ^) echo =============================================================================== set /p userinp= ^> Enter Your Option: if [%userinp%]==[] echo.&echo Invalid User Input&echo.&pause&goto :esdmenu if %userinp% gtr 2 echo.&echo Invalid User Selection&echo.&pause&goto :esdmenu if %userinp%==0 goto :restoreesd if %userinp%==1 goto :exportesd if %userinp%==2 goto :mountesd goto :menu :restoreesd echo. echo =============================================================================== echo. Dism is restoring the online image health using the source Install.esd echo. Please Wait.. This will take 10-15 minutes or more.. echo =============================================================================== %Dism% /Online /Cleanup-Image /RestoreHealth /source:esd:%setupdrv%:\sources\install.esd:1 /limitaccess pause goto :menu :exportesd if not exist "%~dp0bin\wimlib-imagex.exe" (echo.&echo ERROR: Required Wimlib-Imagex Files Not Found.&pause&goto :done) echo. echo =============================================================================== echo. Exporting the Sources\Install.esd into a Source\Install.wim echo. Please Wait.. This will take 5-10 minutes.. echo =============================================================================== md "%~dp0Source" %wimlib% export "%setupdrv%:\sources\install.esd" 1 "%~dp0Source\install.wim" --compress=maximum if not exist "%~dp0Source\install.wim" (echo.&echo ERROR: Required File Export Failed - File Not Found.&pause&goto :done) echo. echo =============================================================================== echo. Dism is restoring the online image health using the exported source media. echo. Please Wait.. This will take 10-15 minutes or more.. echo =============================================================================== %Dism% /Online /Cleanup-Image /RestoreHealth /Source:wim:"%~dp0Source\install.wim":1 /LimitAccess pause goto :menu :Mountesd if not exist "%~dp0bin\wimlib-imagex.exe" (echo.&echo ERROR: Required Wimlib-Imagex Files Not Found.&pause&goto :done) echo. echo =============================================================================== echo. Exporting and Mounting the Source Install.esd echo. Please Wait.. This will take 5-10 minutes.. echo =============================================================================== md "%~dp0Source\Mount" %wimlib% export "%setupdrv%:\sources\install.esd" 1 "%~dp0Source\install.wim" --compress=maximum if not exist "%~dp0Source\install.wim" (echo.&echo ERROR: Required File Export Failed - File Not Found.&pause&goto :Done) %Dism% /Mount-Image /ImageFile:"%~dp0Source\install.wim" /Index:1 /MountDir:"%~dp0Source\Mount" echo. echo =============================================================================== echo. Dism is restoring the online image health using the mounted source media. echo. Please Wait.. This will take 10-15 minutes or more.. echo =============================================================================== %Dism% /Online /Cleanup-Image /RestoreHealth /Source:"%~dp0Source\Mount\Windows" /LimitAccess %Dism% /Unmount-Image /MountDir:"%~dp0Source\Mount" /discard pause goto :menu :opt4 cls echo. echo =============================================================================== echo. Dism is Analyzing the Component Store. echo. If Dism reports Component Store Cleanup Recommended - Select Option 5 or 6 echo. Please Wait.. This will take 5-10 minutes.. echo =============================================================================== %Dism% /Online /Cleanup-Image /AnalyzeComponentStore pause goto :menu :opt5 cls echo. echo =============================================================================== echo. Dism is Cleaning Up the Component Store. echo. Please Wait.. This will take 10-15 minutes.. echo =============================================================================== %Dism% /Online /Cleanup-Image /StartComponentCleanup pause goto :menu :opt6 cls echo. echo =============================================================================== echo. Dism is Resetting the Component Store. echo. Please Wait.. This will take 10-15 minutes or more.. echo =============================================================================== %Dism% /Online /Cleanup-Image /StartComponentCleanup /ResetBase pause goto :menu :opt7 cls echo. echo =============================================================================== echo. System File Checker is scanning the online image for corruption. echo. Please Wait.. This will take 10-15 minutes or more.. echo =============================================================================== if exist "%windir%\logs\cbs\cbs.log" (del /f /q "%windir%\logs\cbs\cbs.log" >nul 2>&1) sfc.exe /scannow findstr /c:"[SR]" %windir%\logs\cbs\cbs.log >"%UserProfile%\Desktop\SFC Details.txt" echo =============================================================================== echo. A SFC Details Report was Created on your Desktop echo =============================================================================== pause goto :menu :opt8 cls echo. echo =============================================================================== echo. Dism is Reverting any Pending Dism Actions. echo. Please Wait.. This will take several minutes.. echo =============================================================================== %Dism% /online /Cleanup-Image /revertpendingactions pause goto :menu :opt9 cls echo. echo =============================================================================== echo. Running Extended Disk Cleanup Manager echo. Check your choice of the Cleanup options and hit ok echo. Please Wait.. This will take several minutes.. echo =============================================================================== %SystemRoot%\System32\Cmd.exe /c Cleanmgr /sageset:65535 %SystemRoot%\System32\Cmd.exe /c Cleanmgr /sagerun:65535 pause goto :menu :opt10 cls echo. echo =============================================================================== echo. Check Disk can not repair disk volumes that are in use.. echo. If the disk volume is in use, Check Disk will prompt you to schedule.. echo. What Disk Volume would you like to scan for errors? echo =============================================================================== set /p volume= ^> Enter Drive Letter: if [%volume%]==[] echo.&echo Invalid User Input&echo.&pause&goto :menu chkdsk %volume%: /F goto :menu :opt11 call "%~dp0bin\showkeyplus.exe" goto :menu :opt12 cls set userinp= echo. echo =============================================================================== echo. Press 0 - TO EXPORT DRIVERS FROM THE CURRENT WINDOWS OS echo. Press 1 - TO EXPORT DRIVERS FROM AN OFF-LINE WINDOWS OS echo =============================================================================== set /p userinp= ^> Enter Your Option: if [%userinp%]==[] echo.&echo Invalid User Input&echo.&pause&goto :menu if %userinp% gtr 1 echo.&echo Invalid User Selection&echo.&pause&goto :opt12 if %userinp%==0 goto :current if %userinp%==1 goto :offline :current echo. echo =============================================================================== echo. EXPORTING CURRENT WINDOWS SYSTEM DRIVERS echo =============================================================================== md "%~dp0Drivers" %Dism% /online /Export-Driver /Destination:"%~dp0drivers" pause goto :menu :offline echo. echo =============================================================================== echo. PLEASE ENTER THE DRIVE LETTER FOR THE OFF-LINE WINDOWS OS echo =============================================================================== set /p drvlttr= ^> Enter Drive Letter: if [%drvlttr%]==[] echo.&echo Invalid User Input&echo.&pause&goto :menu echo. echo =============================================================================== echo. EXPORTING THE OFF-LINE WINDOWS SYSTEM DRIVERS echo =============================================================================== md "%~dp0Drivers" %dism% /Image:%drvlttr%:\ /Export-Driver /Destination:"%~dp0drivers" pause goto :menu :opt13 call "%~dp0bin\windowsmigration.diagcab" goto :menu :opt14 cls echo. echo =============================================================================== echo. Generating a GenuineTicket.xml file - Please Wait... echo. echo. The GenuineTicket.xml file will be placed onto your Desktop... echo. echo. Place File in "ProgramData/Microsoft/Windows/ClipSVC/GenuineTicket" echo =============================================================================== call "%~dp0bin\gatherosstate.exe" timeout /t 5 >nul move "%~dp0bin\GenuineTicket.xml" "%UserProfile%\Desktop" goto :menu :opt15 start TechBench by WZT (v4.1.1) goto :menu :opt16 start UUP dump goto :menu :done if exist "%~dp0Source" rmdir /s /q "%~dp0Source" cls echo. echo =============================================================================== echo. echo. IMAGE HEALTH echo. WRITTEN BY KYHI echo. JULY 06,2016 echo. echo =============================================================================== if exist "%windir%\logs\dism\dism.log" ( echo. The Dism Details Report will be copied onto your Desktop echo =============================================================================== copy "%windir%\logs\dism\dism.log" "%UserProfile%\Desktop\Dism Details.txt") timeout /t 5 >nul exit
Test Admin Rights.bat
Code:@echo off title Test Admin Rights color 17 set "params=%*" setlocal EnableDelayedExpansion cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || ( echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit /B ) %windir%\system32\reg.exe query "HKU\S-1-5-19" 1>nul 2>nul || ( echo. echo ============================================================ echo ERROR: Run the script as administrator. echo ============================================================ echo. echo Press any key to exit... pause >nul goto :eof ) Echo You Have Admin Command Prompt echo. Pause exit
Hello @Kyhi,
Out of curiosity, if your vbs method sets Admin when running the .cmd file directly [ automatically ], why do you have? . . .
Thanks in advance.Code:echo ============================================================ echo ERROR: Run The Script As Administrator. echo ============================================================
It rechecks to verify and if not then error message...
If I drop a file on cmd to open file...
I get vbs error (this is a new issue recently) - when dropping a file those are the %params%
Once I close the message box - you see error message - and notice that "Administrator" missing from title
If you test Matt's method - it will show c:\windows\system32 (system prompt) versus c:\users\name (user prompt) - Notice my "Administrator" prompt using c:\windows\system32 (system prompt)