Categories
Operating Systems

Testing and Elevating Admin Privileges in Windows 7 / Windows 2008

Introduction

Due to UAC , many processes, including many installations, must be run from an elevated privilege level, even when you are logged in as an administrator.

Testing for Privileges

The following code in an installation script will test to see if the script is operating

::Test whether script has Admin Privileges/is elevated
at > nul:
If "%errorlevel%"=="0" (
  echo You are Administrator
) else (
  echo You are NOT Administrator. Exiting...
  Ping 127.0.0.1 > nul: 2>&1
  Exit /B 1
)

Elevating a Session

You can run any executable by right-clicking it and selecting Run as Administrator.  However, you may find that you want to simply elevate your session.  (Think before you do this, there's a reason they have UAC.)

The following process details how to elevate a session;

  • Log in as admin
  • Open an elevated CMD window
  • Right-Click on Command Prompt in the Start menu and click Run as Administrator.
  • Kill the explorer proccess from Task Manager or enter the following command at the elevated command prompt. (/F forcefully terminates the process and /IM represents the 'image name' of the process to be killed, i.e. explorer.exe )
    • taskkill /F /IM explorer.exe
    • In the elevated CMD window, start a new explorer process explorer.exe
    • Close the elevated CMD window
    • Since the explorer process is now running elevated, subsequent operations should not require elevation.

In fewer words:

Log in as admin
From an elevated command window enter the following 3 commands:

taskkill /IM explorer.exe /F
explorer
exit