Script to Check Win 7 users for Admin Rights

Hewlett-packard / Sd2100
December 10, 2009 at 20:42:10
Specs: Windows 7, 1024
ok guys i have this code that works in Vista and XP but not windows 7

for /f "delims=" %%a in ('2^>^&1 ^>nul defrag -? %systemdrive%') do set notadmin=%%a
if defined notadmin (
echo Not admin!
exit /b

the issue is that in windows 7 the -? wont work so what else could i use in place of it to make it work out for me?

i use this code to check admin right and tells the user if they are runing the batch with Admin rights or not..

**EDIT** forgot to mention that the - a takes forever because it analizes the drive and i dont want the batch to do that. so in vista and xp the -? worked out great but in windows 7 the code is just skiped over.

thank you for you help!

See More: Script to Check Win 7 users for Admin Rights

Report •

December 10, 2009 at 22:35:44
I don't have win7, but generally the standard help switch is "/?"(will work on xp/vista as well), perhaps they stopped supporting "-?"?

So basically try "/?" instead of "-?".

Edit: To check if the trick will even work can you post the output from a non-admin user of the below code:

defrag /? > nul

Batch Variable how to

Report •

December 10, 2009 at 22:40:39
Thank you, I will try that when i get a chance and i will post back my finding.

thank you again.


Report •

December 11, 2009 at 05:41:26
DEBUG has changed a lot between XP and Win7 Beta; you might need to find another command, like FSUTIL.

Report •

Related Solutions

December 11, 2009 at 06:09:19
Razor2.3 do you mean "defrag" rather that "debug"?

Fsutil could work but I don't know if uac would get involved in vista/7....

In xp "fsutil > nul" leaves errorlevel 1 on non-admin and errlevel 0 on admin, stderr doesn't seem to be used on xp so it's errorlevel only(may it be consistant).

I remember suggesting "echo exit|diskpart" in brokey01's last thread regarding this issue, which apparently invoked uac in vista.

Batch Variable how to

Report •

December 11, 2009 at 13:55:10
well it seems that code doesnt even work in windows 7 but does in XP and Vista not sure why, it just 'skips' over it and goes right into the batch file.

now i guess what im looking for is a code to work in windows 7 that check if the user selected 'Run As Administrator' and if not have it error.

they code i had posted does that for vista but not win7.

thank you for all you help.

Report •

December 12, 2009 at 11:58:39


this works

@echo off
REM Vista
VER | FIND "Version 6.1." > nul
REM Do OPENFILES to check for administrative privileges
ECHO.Right click on this bat file and select 'Run as administrator'.

but if you are the 'true' administrator it will just do a UAC prompt but any other admin accoutn will ask you to run as administrator.

thank you again for all your guys help though, you got me on the right track!

Report •

Ask Question