How can I create a batch file to run commands as admin?

0
4
Asked By TechWhiz88 On

Hey there! I'm looking for a way to automate two command lines that I run every time I start my computer. Here they are:

1. `netsh wlan set autoconfig enabled=no interface="Wi-Fi"`
2. `netsh wlan set autoconfig enabled=yes interface="Wi-Fi"`

These commands help me reduce lag spikes in games by toggling my Wi-Fi antenna settings, and I want to make my life easier by avoiding the manual process of entering them each time. Is it possible to create two batch files, one for each command, that will automatically run with administrative privileges when I click on them? Any help would be greatly appreciated! Thanks!

4 Answers

Answered By BatchGuru123 On

You can create a batch file and then set it to run as an administrator. To do this, make a shortcut for your batch file and in the properties, there should be an option to run it as admin—probably under the Advanced settings. Just make sure to save your commands properly in the .bat file first!

LazyCoder99 -

Awesome, I tried it and it worked like a charm. Thanks a lot!

Answered By AdminTechie On

For those who might be interested, here's a script that checks for admin permissions and prompts for them if not. It’s a bit more complex but gives you more control:

```batch
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
>nul 2>&1 "%SYSTEMROOT%SysWOW64cacls.exe" "%SYSTEMROOT%SysWOW64configsystem"
) ELSE (
>nul 2>&1 "%SYSTEMROOT%system32cacls.exe" "%SYSTEMROOT%system32configsystem"
)

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%getadmin.vbs"
set params= %*
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params:"=""%", "", "runas", 1 >> "%temp%getadmin.vbs"
"%temp%getadmin.vbs"
del "%temp%getadmin.vbs"
exit /B

:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
```

Answered By CommandMaster5 On

Actually, you should only need one batch file for both commands to run in order. Just open any text editor, write both commands in it, then save it as a .bat file. After that, create a shortcut to the file and set it to run as admin. This way, it'll execute both at once when you click it!

QuickFixee -

Using a single file is definitely more efficient! Just remember to add a pause at the end if you want to catch any error messages before the command window closes.

Answered By ScriptSmith On

I often use commands like this at work, but they prompt for a password. You might consider including a password with the runas command if it fits your needs, though I change my admin passwords frequently, so I don't rely on it too much:

`runas /netonly /user:YOURUSERNAME "mmc %SystemRoot%system32dsa.msc"`
`runas /netonly /user:YOURUSERNAME "cmd"`
Just sharing in case it’s helpful!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.