Running as Administrator
Many commands in this section only read information, and any user may run them. Changing the system is different: stopping a service, writing under HKLM, creating a task that runs as SYSTEM, editing C:\Windows or C:\Program Files. Those need administrator rights, and on Windows being in the Administrators group is not enough: your window must also be elevated. This lesson explains why, how to open an elevated prompt, how a batch file can tell whether it is elevated, and how to run a command as another user.
UAC and the split token
Since Windows Vista, User Account Control (UAC) gives an administrator two access tokens at logon:
ada logs in (member of Administrators)
|
+--> filtered token (Medium integrity) used by Explorer and everything you start normally;
| the Administrators group is present but "deny only"
|
+--> full token (High integrity) used only after a UAC prompt ("Run as administrator")Every normal window, including a Command Prompt you open from the Start menu, runs with the filtered token. When something needs the full one, Windows shows the UAC consent prompt ("Do you want to allow this app to make changes to your device?"), and only the program you approved runs elevated. Standard users get a credential prompt instead and must type an administrator's password. The Windows security model behind this is covered in Windows Security.
What a non-elevated prompt gets when it tries anyway:
C:\Users\ada>sc stop Spooler
[SC] OpenService FAILED 5:
Access is denied.
C:\Users\ada>reg add HKLM\SOFTWARE\TadDemo /f
ERROR: Access is denied.
C:\Users\ada>net session
System error 5 has occurred.
Access is denied.Sample output. Error 5 (ERROR_ACCESS_DENIED) is the signature of "not elevated".
Opening an elevated Command Prompt
| Way | How |
|---|---|
| Start menu | type cmd, then Ctrl+Shift+Enter (or right-click → Run as administrator) |
| Power-user menu | Win+X → Terminal (Admin), then open a Command Prompt tab |
| Run dialog | Win+R, type cmd, press Ctrl+Shift+Enter |
| Task Manager | Run new task → tick Create this task with administrative privileges |
| Windows Terminal | settings of a profile → Run this profile as Administrator |
You can recognise an elevated window by its title, Administrator: Command Prompt, and by its start folder, C:\Windows\System32 instead of your profile folder. Remember to cd somewhere sensible before creating files.
sudo for Windows
Windows 11 24H2 added a sudo command. Turn it on in Settings → System → For developers → Enable sudo (or from an elevated prompt with sudo config --enable normal), then prefix a single command:
C:\Users\ada>sudo sc stop Spooler
SERVICE_NAME: Spooler
TYPE : 110 WIN32_OWN_PROCESS (interactive)
STATE : 3 STOP_PENDING
...Sample output (after a UAC prompt). Three modes exist: in a new window (forceNewWindow), with input disabled (disableInput, output shows inline but you can't type into the elevated command) and inline (normal, behaves like Linux sudo). It still uses the UAC prompt, not your password typed in the terminal.
Is this script elevated?
A batch file that changes the system should check first and stop with a clear message, instead of half-working and spewing "Access is denied". There is no built-in variable for it, so scripts run a harmless command that only succeeds when elevated and look at errorlevel:
@echo off
net session >nul 2>&1
if errorlevel 1 (
echo This script must be run as administrator.
exit /b 1
)
echo Elevated, continuing...net session lists network sessions, which requires elevation; fltmc >nul 2>&1 (lists file-system filter drivers) is a popular alternative that also works when the Server service is disabled.
A third way reads the integrity level from whoami /groups: an elevated token contains the label High Mandatory Level (S-1-16-12288), a filtered one Medium Mandatory Level (S-1-16-8192). Here are the relevant lines from both windows of the same user (sample output, shortened):
C:\Users\ada>whoami /groups | findstr /c:"Mandatory" /c:"BUILTIN\Administrators"
BUILTIN\Administrators Alias S-1-5-32-544 Group used for deny only
Mandatory Label\Medium Mandatory Level Label S-1-16-8192C:\Windows\System32>whoami /groups | findstr /c:"Mandatory" /c:"BUILTIN\Administrators"
BUILTIN\Administrators Alias S-1-5-32-544 Mandatory group, Enabled by default, Enabled group, Group owner
Mandatory Label\High Mandatory Level Label S-1-16-12288Note Group used for deny only: the filtered token still lists Administrators, but the group can only be used to deny access, never to grant it. The playground can't elevate, so this runnable version checks the two saved outputs with the same findstr test a real script would pipe whoami /groups into:
@echo off
(
echo BUILTIN\Administrators Alias S-1-5-32-544 Group used for deny only
echo Mandatory Label\Medium Mandatory Level Label S-1-16-8192
) > normal.txt
(
echo BUILTIN\Administrators Alias S-1-5-32-544 Mandatory group, Enabled by default, Enabled group, Group owner
echo Mandatory Label\High Mandatory Level Label S-1-16-12288
) > elevated.txt
for %%w in (normal.txt elevated.txt) do call :check %%w
goto :eof
:check
findstr /c:"S-1-16-12288" "%~1" >nul
if errorlevel 1 (
echo %~n1: NOT elevated - admin tasks will fail with error 5
) else (
echo %~n1: elevated
)
exit /bnormal: NOT elevated - admin tasks will fail with error 5
elevated: elevated
On a real PC the check is one line: whoami /groups | findstr /c:"S-1-16-12288" >nul && echo elevated || echo not elevated. Searching for the SID instead of the words High Mandatory Level keeps it working on non-English Windows.
A script that elevates itself
Users often double-click a batch file. Instead of telling them to right-click it, the script can restart itself elevated: PowerShell's Start-Process -Verb RunAs triggers the UAC prompt. The original copy then exits. To keep the arguments, the script passes %* along. The playground can't show a UAC prompt, so this example prints the command it would run (and fakes the elevation check with a variable):
@echo off
setlocal
set "SELF=%~f0"
set "IS_ADMIN=no"
call :main /target D:\backup /verbose
goto :eof
:main
if "%IS_ADMIN%"=="no" (
echo Not elevated. Relaunching with:
echo powershell -NoProfile -Command "Start-Process -FilePath '%SELF%' -ArgumentList '%*' -Verb RunAs"
exit /b 0
)
echo Running elevated with arguments: %*
exit /b 0Not elevated. Relaunching with:
powershell -NoProfile -Command "Start-Process -FilePath 'C:\tad\main.bat' -ArgumentList '/target D:\backup /verbose' -Verb RunAs"
exit /b 0 inside the if block ends :main right away, so the "Running elevated" line is only reached when IS_ADMIN is not no. In a real script, replace the second echo with the powershell command itself and replace set "IS_ADMIN=no" with the net session test from above. When the user accepts the UAC prompt, the new elevated copy passes the check and does the work.
Two practical notes: the elevated copy starts in C:\Windows\System32, so the script should cd /d "%~dp0" right after the check; and arguments containing quotes or & need extra care when they travel through PowerShell, so keep self-elevating scripts' arguments simple.
runas: a different user
runas starts a program as another account. It's useful for testing with a standard user or using a separate admin account, but note what it is not: it does not elevate. If the other account is an administrator, the program still gets its filtered token under UAC.
C:\Users\ada>runas /user:TAD-PC\helpdesk cmd
Enter the password for TAD-PC\helpdesk:
Attempting to start cmd as user "TAD-PC\helpdesk" ...
C:\Users\ada>runas /netonly /user:CORP\ada.admin "mmc dsa.msc"
Enter the password for CORP\ada.admin:
Attempting to start mmc dsa.msc as user "CORP\ada.admin" ...Sample session. The password is typed at the prompt (it can't be passed on the command line, by design). /netonly uses the other credentials only for network access, which is handy for managing a domain from a non-domain PC. /savecred stores the password so later runas calls don't ask, but then anyone using your account can run things as that user, so avoid it.
| Need | Use |
|---|---|
| one admin command now | elevated prompt, or sudo on Windows 11 24H2+ |
| a script that needs admin | check with net session/fltmc, then stop or self-elevate |
| a program as another user | runas /user:... |
| a task as SYSTEM | schtasks /create ... /ru SYSTEM from an elevated prompt (schtasks) |
| PowerShell | Start-Process cmd -Verb RunAs; check with ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole('Administrators') |
Tip: work in a normal window and open an elevated one only for the commands that need it. A permanently elevated window turns every typo (
rd /s /qin the wrong folder) into a system-wide problem. On Linux the same idea is "usesudoper command, don't log in as root" (Users & Groups).
Test yourself
Ada is in the Administrators group, opens Command Prompt from the Start menu and runs sc stop Spooler. She gets error 5. Why?
- The Spooler service can't be stopped
- Under UAC her normal window uses a filtered token; she needs an elevated prompt
- sc only works in PowerShell
- She must reboot first
Which line in whoami /groups shows that the current window is elevated?
Mandatory Label\High Mandatory Level(S-1-16-12288)BUILTIN\Administrators ... Group used for deny onlyMandatory Label\Medium Mandatory Level(S-1-16-8192)Everyone
What does runas /user:admin2 cmd do on a PC with UAC enabled?
- Opens an elevated prompt without any password
- Starts cmd as the user admin2 (after asking for admin2's password), but not elevated
- Makes the current window elevated
- Adds admin2 to the Administrators group