Welcome to MSDN Blogs Sign in | Join | Help

Setting color for *all* CMD shells based on admin/elevation status

In my RunAs... and MakeMeAdmin posts, I recommend making your admin command shells visually different to set them apart from non-admin ones.  You can change the default console window color on a per-account basis, but that doesn't help when the same account may be used in both admin and non-admin contexts (such as with Vista's UAC admin-approval mode).  You can use the cmd.exe /T command-line option, or its built-in COLOR command, but it works only if you remember to use it each and every time.

Here's a way to make the differentiation happen with a one-time, one-line configuration change on your system, that will work on all CMD.EXE shells you run.  The idea is to run a non-destructive command that requires admin privileges from a CMD autorun location, test for success and set the console's color accordingly.  You can also change the title at the same time.

This can probably use some refinement.  For the non-destructive admin operation on Windows XP/2003, I suggest "bootcfg /query"; on Windows Vista, I suggest "bcdedit /enum".  The autorun location I've been playing with is:

    [HKLM\Software\Microsoft\Command Processor]
    "AutoRun" (REG_SZ)


The command syntax you can set the "AutoRun" value to for Windows XP/2003 is:

    bootcfg /query >nul 2>nul && (color FC && title ADMIN) || (color 07 && title NONADMIN)

and for Windows Vista, set it to:

    bcdedit /enum >nul 2>nul && (color FC && title ADMIN) || (color 07 && title NONADMIN)

Any output or error message is redirected to "nul" so you don't see it.  If the command succeeds (&&), you're running with admin/elevated privileges; the console color will change to bright-red-on-white (FC) and the title changed to "ADMIN".  If the command fails (||), the console color will be white-on-black (07) and the title changed to "NONADMIN".  Feel free to change the colors or titles to suit your taste.

All that stuff works only for CMD.EXE.  For Windows PowerShell, take a look at these:

http://www.interact-sw.co.uk/iangblog/2007/02/09/pshdetectelevation
http://www.leastprivilege.com/AdminTitleBarForPowerShell.aspx

Also for PowerShell -- Staffan Gustafsson converted MakeMeAdmin to a PowerShell script:

http://groups.archivesat.com/Windows_PowerShell/thread246430.htm

[2007-06-25:  Update posted here.]

Published Thursday, February 22, 2007 11:44 PM by Aaron Margosis
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Aaron Margosis' WebLog : Table of contents, Aaron Margosis' non-admin blog

# re: Setting color for *all* CMD shells based on admin/elevation status

Friday, February 23, 2007 2:20 AM by Pavel Lebedinsky

You could also do something like this:

cacls %windir%\system32\config\systemprofile >nul 2>nul && echo admin || echo non-admin

This should work on both XP and Vista.

# re: Setting color for *all* CMD shells based on admin/elevation status

Friday, February 23, 2007 11:46 AM by Harris

Aaron:

Just an FYI, there's an error in MakeMeAdmin.ps1 that you referenced.  The "if" statement in the SuPowershell function should read "if($SuAccount)" not "if(!$SuAccount)".

Otherwise, that's awesome, thanks for point it out!

Harris

Harris -- thanks, yeah, that didn't look right to me either.  I tried to post your comments as a reply on that page, but I don't know whether it went through or not.

-- Aaron

# A much simpler way, if you habitually use the *same* admin account

Friday, February 23, 2007 5:37 PM by Ross Presser

Put the bare command

 COLOR FC

in HKCU\software\microsoft\Command Processor *for the admin account only*. No test required.

Of course if you have several admin accounts that you use -- e.g. a local one and a domain one -- put it in each one.

On my own machine I sometimes run in an account which is not a Domain admin but is an admin of the local machine, and I need to distinguish. The BOOTCFG trick won't distinguish between them .. so I would depend on HKCU.

Ross:  The problem here is that with MakeMeAdmin on XP/2003 and with UAC's Admin-Approval Mode on Vista, you can have two CMD windows side by side running as the same user, one with admin/elevated permissions and the other not.  Tying to HKCU won't help.

-- Aaron

# Correctness of MakeMeAdmin.ps1

Sunday, February 25, 2007 3:20 PM by Staffan Gustafsson

I have to point out that the representation groups.archivesat.com of the script I wrote is incorrect.

You can see a correct version at via google groups at http://groups.google.com/group/microsoft.public.windows.powershell/browse_frm/thread/bc7fb9969347dd4a/84ab68ebb0f486f3?lnk=st&q=makemeadmin.ps1&rnum=1#84ab68ebb0f486f3

On ArchiveSat, the critical lines says:

if (!$SuAccount)

   $StartInfo = new-object System.Diagnostics.ProcessStartInfo

On Google:

if (!$SuAccount){

       return

   }

Makes quite a difference, doesn't it? :)

/Staffan

# re: Setting color for *all* CMD shells based on admin/elevation status

Monday, February 26, 2007 5:29 PM by Ross Presser

>> Ross:  The problem here is that with MakeMeAdmin on XP/2003 and with UAC's Admin-Approval Mode on Vista, you can have two CMD windows side by side running as the same user, one with admin/elevated permissions and the other not.  Tying to HKCU won't help. <<

Aaron: Good point, and if I used MakeMeAdmin like I'm supposed to, I'd have hit that myself. :-) Well, is there some kind of test that can distinguish between local machine admin and domain admin?

Ross:  Perhaps something like "dir \\mydc\c$", replacing "mydc" with the name of a domain controller that's always online?  Or if it works in your environment, "dir \\%USERDNSDOMAIN%\c$"?  (Note that %USERDNSDOMAIN% should expand to nothing for local accounts -- which works for this purpose since you want local accounts to fail the test.)

Let me know whether this works for you -- I plan to post an update on this topic in the next few days.

HTH

-- Aaron

# re: Setting color for *all* CMD shells based on admin/elevation status

Tuesday, February 27, 2007 6:50 PM by anonymous

Very bad thing I found out: it turns every Cmd Console into ANSI mode, yet all subsequently invoked Cmd instance pertain UNICODE.

Test case:

for /f "delims=" %i in ('dir /s /b /a-d') do echo "%i"

This will break for the first file, since the cmd instance invoking the 'dir' command returns unicode, but the calling shell is ANSI and thus stumbles upon the Unicode BOM at the beginning.

Can you provide more clarification?  Every CMD is ANSI by default.  The only way I know of to make its pipe/redirection output be Unicode is by starting CMD with the /U switch.  I tried starting command shells with /U both with and without the AutoRun value and found no difference in the output -- and in particular, I saw no BOM in any of the output.  The specific test I tried was to run this command in the %TEMP% folder:

(for /f "delims=" %i in ('dir /s /b /a-d') do echo "%i") > dir.txt

-- Aaron

# re: Setting color for *all* CMD shells based on admin/elevation status

Friday, March 16, 2007 1:40 PM by Ronnie Miller

Here is a way to change the color if you are running under a Domain Administrator context.

"c:\program files\windows resource kits\tools\ifmember.exe" "mydomain\domain admins" >nul 2>nul && (color 07) || (color FC)

-or you can copy ifmember.exe to somewhere in your path.  Of course, change mydomain to the name of your domain.

FYI to readers who don't already know this:  "ifmember.exe" comes with the Windows Server 2003 Resource Kit Tools, freely downloadable from microsoft.com.

-- Aaron

# re: Setting color for *all* CMD shells based on admin/elevation status

Tuesday, March 20, 2007 12:47 PM by Chris R

I just wanted to let you know there are some side affects that cause things to break when using your bootcfg trick.  

For example we have a Visual Studio 2003 project which runs the following command line function as a Pre-Build Event.  

xcopy ..\..\exe_dir ..\..\debug /d /e /y /EXCLUDE:..\..\exe_dir\exclude.txt

With the trick in place it xcopy fails during the build, although the same code works fine from a command window.

Visual Studio executes the command by generating the following batch file.

Creating temporary file "r:\MyProject\Release\obj\MyDll\BAT000002.bat" with contents

[

@echo off

xcopy ..\..\exe_dir ..\..\release /d /e /y /EXCLUDE:..\..\exe_dir\exclude.txt

if errorlevel 1 goto VCReportError

goto VCEnd

:VCReportError

echo Project : error PRJ0019: A tool returned an error code from "Copy exe_dir to release"

exit 1

:VCEnd

]

Creating command line "r:\MyProject\Release\obj\MyDll\BAT000002.bat"

When run the output is:

Copy exe_dir to release

0 File(s) copied

MyDll : error PRJ0002 : error result returned from 'r:\MyProject\release\obj\MyDll\bat000002.bat'

Removing the AutoRun value fixes the problem, so I don't expect you to troubleshoot this. I just want to warn others.  

Chris R:  Thanks, I've been meaning to post a follow-up.  It's actually the COLOR command in the AutoRun value that messes up some Visual Studio build events.  It happens for Visual Studio 2005 as well.

-- Aaron

# re: Setting color for *all* CMD shells based on admin/elevation status

Thursday, April 05, 2007 1:18 PM by Jeff Chadbourne

Thanks Chris R!  This was driving me nuts. I was getting Pre-build errors too, and couldn't figure out why.

I was able to fix this by adding:

& echo > nul

which will run the echo after either the admin or non-admin commands run.  This will return errorlevel 0 to VS.Net which will allow the pre-build event to complete.

# Follow-up on "Setting color for *all* CMD shells based on admin/elevation status"

Monday, June 25, 2007 6:14 PM by Aaron Margosis' "Non-Admin" WebLog

Improvements on my earlier post about setting color and title for CMD (and PowerShell) windows, based on admin/elevation status

# Follow-up on "Setting color for *all* CMD shells based on admin/elevation status"

Wednesday, June 27, 2007 9:50 PM by Aaron Margosis' "Non-Admin" WebLog

Improvements on my earlier post about setting color and title for CMD (and PowerShell) windows, based on admin/elevation status

# Ламинат цены

Sunday, August 24, 2008 7:34 AM by ламинат

1fThank's.3k I compleatly disagree with last post .  viw

<a href="http://skuper.ru">паркет</a> 3b

# re: Setting color for *all* CMD shells based on admin/elevation status

Monday, October 13, 2008 1:57 PM by Czerno

Hi, Aaron! I wish to thank you for your great "non admin" postings !

What command would you suggest for use on Windows 2000 (where bootcfg isn't available) to distinguish between admin/non admin status ?

Also, I need something which will not depend on NTFS permissions.

Thanks once more

--

Czerno

# re: Setting color for *all* CMD shells based on admin/elevation status

Monday, October 13, 2008 2:10 PM by Czerno

re my above question, found a solution viz

use the command line registry tool reg.exe,

for instance :

reg update software\mytrick=1

There might be more elegant ways...

Regards

--

Czerno

# re: Setting color for *all* CMD shells based on admin/elevation status

Friday, October 30, 2009 5:25 AM by Ross Presser

In Windows 7, BOOTCFG always always returns errorlevel 1, even when it succeeds when running as Administrator. Here's a refinement:

(BOOTCFG /query 2>nul |FINDSTR Entries >nul) && (COLOR 47) || (COLOR 07 & echo>nul)

This uses FINDSTR to search the BOOTCFG output for the word "Entries", which it should always have if it succeeds. It also adds the "echo>nul" for the non-admin case, to clear the errorlevel.

Windows 7 already adds the "Administrator" to the title bar, but if you still need the color change, here it is.

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker