Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to determine in Win 7 if running from elevated command prompt?

597 views
Skip to first unread message

John Gray

unread,
Oct 16, 2010, 12:08:15 PM10/16/10
to
Does anyone know a neat programmatic way of determining, from within a
batch file running on Windows 7, whether it is running in elevated
mode (run as Administrator) or just from a normal command prompt?

The only distinguisher I have found is that in elevated mode the
default current directory is C:\Windows\system32 whereas for a normal
command prompt the current directory is C:\Users\username...

foxidrive

unread,
Oct 16, 2010, 12:16:50 PM10/16/10
to

Someone else posted this style of command to determine if admin is being
used. I think it was diskpart...


set "admin="
diskpart /?>nul&& set admin=1
if defined admin "do something"


--
Regards,
Mic

John Gray

unread,
Oct 16, 2010, 1:04:54 PM10/16/10
to

That would be fine, but DISKPART /? appears to return an errorlevel of
999999999 (yes!).
And in a normal command prompt, you get the UAC prompt.

I can see the principle, but would prefer to find a command which
gives the expected errorlevel and which doesn't pop up a UAC in normal
mode.

John Gray

unread,
Oct 17, 2010, 6:46:30 AM10/17/10
to

The following appears to work successfully in distinguishing whether a
batch file is running in Elevated Mode or in Normal Mode in Windows
7. It has been cribbed from ten.nigriv and modified slightly

:: method (applicable to Windows 7, and maybe Vista)
:: try to write a zero-byte file to a system directory
:: if successful, we are in Elevated mode and delete the file
:: if unsuccessful, avoid the "Access is denied" message

:: arbitrary choice of system directory and filename
set tst="%windir%\$del_me$"

:: the first brackets are required to avoid getting the message,
:: even though 2 is redirected to nul. no, I don't know why.
:: next command should be on a single line
(type nul>%tst%) 2>nul && (del %tst% & set elev=t) || (set elev=)

if defined elev (echo Elevated mode) else (echo Normal mode)

Frank P. Westlake

unread,
Oct 20, 2010, 11:05:42 AM10/20/10
to
"John Gray"
news:2d915698-6613-404d...@n26g2000yqh.googlegroups.com...

> Does anyone know a neat programmatic way of determining, from within a
> batch file running on Windows 7, whether it is running in elevated
> mode (run as Administrator) or just from a normal command prompt?


Mine always comes up as Administrator and I've forgotten how to become
little again, so I can't experiment. Isn't there something in WHOAMI
that indicates the elevation?

Frank


0 new messages