Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: programmatic way with BAT file determining if elevated command prompt

16 Oct 2010   #1
JohnGray

Windows 7 Pro 64-bit
 
 
programmatic way with BAT file determining if elevated command prompt

Is there any programmatic way within a BATch file of determining whether it has been started from an elevated command prompt (run as Administrator) or a normal command prompt?

I note the difference between the Current Directory at start of the BATch file, but wonder if there is a better way.

When in elevated mode I don't get a UAC prompt when using DISKPART.
The UAC prompt only happens when running it in a normal command prompt window.

Are you aware of any command which simultaneously would
  • give a different errorlevel when executed in an elevated command prompt than in a normal command prompt, and
  • not produce a UAC prompt in normal mode?
If this is the case, unless I can find something to distinguish between elevated command prompts and normal command prompts, then I will have to try the %cd% variable at the start of BATch files, to see whether it is consistent even across user modifications of the command prompt shortcut's Target Directory...


My System SpecsSystem Spec
.

17 Oct 2010   #2
JohnGray

Windows 7 Pro 64-bit
 
 

I can answer my own question, as below
Code:
@echo off

:: +----------+
:: I Elevated I  Test whether this BATch file / Command Prompt
:: +----------+    in running in Elevated mode or Normal mode

setlocal

:: 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.
(type nul>%tst%) 2>nul && (del %tst% & set elev=t) || (set elev=)

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

endlocal
My System SpecsSystem Spec
Reply

 programmatic way with BAT file determining if elevated command prompt




Thread Tools





Similar help and support threads
Thread Forum
Elevated Command Prompt Shortcut
How to Create an "Elevated Command Prompt" Shortcut in Windows 7 and Windows 8 This tutorial will show you how to create a shortcut that will open an elevated command prompt with administrator rights in Windows 7 and Windows 8. When you use the elevated command prompt shortcut, you may be...
Tutorials
Elevated Command Prompt
How to Open an Elevated Command Prompt in Windows 7 This will show you how to open an elevated command prompt with administrator rights (Run as administrator) instead of the default command prompt without administrator rights in Windows 7. If you opened the elevated command prompt from a...
Tutorials
Elevated command prompt over SSH
Not sure if this is the proper forum for my question, but here it goes. I'm using a SSH server to remotely access my notebook from work. It works well for SFTP and remote command line access using a dedicated, non-admin, user account. Problem is when I want to run something that requires...
Network & Sharing
Tip for Elevated Command Prompt
This probably is not new to many, but I find it useful. There are times I want an elevated command prompt. I usually type in "cmd", right click on it and run as administrator. If you need an elevated command prompt now and then, simply create a shortcut to "cmd". Right click on the shortcut,...
General Discussion
How to open Elevated Command Prompt?
Hi friends ! Uber Philf told me to start a new thread with my problem. I cannot open an Elevated Command Prompt, Dont know why. I know that I can open it in two alternative ways : 1). Win+R, type cmd, hit enter eith ctrl+shift pressed ; and 2). start > all programs > accessories > command prompt >...
Performance & Maintenance
elevated command prompt fails
If i try and use cmd from within the start menu ,in accessories and right click ,then "Run as administrator",all i am granted is normal cmd usage e.g(C:\Users\(User Name ).Now,i then pinned cmd to the start menu and again i tried to "run as administrator",but this time cmd fails to open at all. ...
General Discussion

Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 03:52.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App