Batch file to run cmds only if user is NOT an administrator

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SpyGob
Posts: 2
Joined: 01 Aug 2011 20:23

Batch file to run cmds only if user is NOT an administrator

#1 Post by SpyGob » 01 Aug 2011 21:04

Hi there,

I'm trying to create a login script that will modify certain registry keys based on the user's credentials. What I want is for the batch file to look for the current user and modify the registry only if it finds that the user is not an administrator. If it finds that the current user is an administrator, then it shoud stop executing commands go to exit.

Any help will be greatly appreciated.

SpyGob

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: Batch file to run cmds only if user is NOT an administr

#2 Post by Acy Forsythe » 02 Aug 2011 08:02

You can use this in XP and Windows 7...


Code: Select all

IF NOT "%USERNAME%"=="administrator" (
    Do some commands for users
  ) ELSE (
   Do some other commands
   Exit /b
)


OR

Code: Select all


IF /I "%USERNAME%" NEQ "administrator" (
    Do your commands for normal users
 ) ELSE (
     Do other commands
)


Actually after thinking about it for a few more seconds...

I think this is what you actually want:

Code: Select all


IF /I "%USERNAME%" EQU "administrator" (exit /b)

Commands for users


Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch file to run cmds only if user is NOT an administr

#3 Post by Ed Dyreen » 02 Aug 2011 08:17

'
You are mistaken Acy...; Users do not have to be named Administrator for them to be Administrators !

Use wmic, net use or registry. :)

Code: Select all

net localgroup Administrators

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: Batch file to run cmds only if user is NOT an administr

#4 Post by Acy Forsythe » 02 Aug 2011 08:53

Ed you know... You're right :) I didn't think about that... :oops:

SpyGob
Posts: 2
Joined: 01 Aug 2011 20:23

Re: Batch file to run cmds only if user is NOT an administr

#5 Post by SpyGob » 02 Aug 2011 22:51

Thank you both for your quick responses.

Acy, what you gave is exactly what I needed. What I actually meant was if the user was the local administrator with the username "administrator'.

Thank you so much. You have taken away a huge headache.

God Bless.

SpyGob

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: Batch file to run cmds only if user is NOT an administr

#6 Post by Acy Forsythe » 03 Aug 2011 08:47

You're welcome SpyGob, but Ed's way is more "accurate" incase you do have users that have administrative rights. This usually occurs in the home/ home office where the first username registered on the PC has administrative rights and in businesses where the "administrator" account is disabled for security reasons, so admin users login with their own username/password.

Post Reply