Skip to content

Instantly share code, notes, and snippets.

@Broxzier
Created July 3, 2023 08:30
Show Gist options
  • Save Broxzier/240cd1a6f5b89fde960949344faa1401 to your computer and use it in GitHub Desktop.
Save Broxzier/240cd1a6f5b89fde960949344faa1401 to your computer and use it in GitHub Desktop.
Request admin permisions from batch
@echo off
:: Ensure we're admin by checking if the last arguent is "asadmin", and if not, restart as administrator with this argument set.
for %%a in (%*) do set last=%%a
if NOT "%last%" == "asadmin" (
powershell start -WindowStyle Minimized -verb runas '%0' '%* asadmin'
exit
)
:: Ensure the working directory is the same folder as where this script is located
cd /D "%~dp0"
:: Place the rest of the script here. This will be run with admin privileges.
@Broxzier
Copy link
Author

Broxzier commented Jul 3, 2023

Note that this will cause a UAC prompt to appear, which will prevent the script from running until accepted. To disable this notification, head over to User Account control Settings > Choose when to be notified about changes to your computer, and pull the slider down to Never notify.

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