Jump to content

Add new users via batch


Recommended Posts

I do not want to add users via NET USER or OOBEINFO.INI methods. I want to add them later via a batch file.

I am getting really frustrated. :realmad: I have been working on this batch file for hours and I can't seem to make it right. It seems like everything I do creates another error. I am going in circles. grrrr

@Echo Off
Echo.
Title Add New Users
Echo.
set path="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion"
set /p addusers=Add New Users? Y/n:
if "%addusers%"=="y" (goto proceed)
Echo.
Echo Note: The first added user will be set as default user
set count=1
:Proceed
Echo.
:Name
set /p name=User Name?
if "%name%"=="" Echo. & Echo Invalid Entry & Echo. & goto Name
Echo.
:Usertype
Echo User Groups:
Echo.
Echo 1. Administrators: Have complete and unrestricted access to the computer/domain.
Echo 2. Power Users: Possess most administrative powers with some restrictions.
Echo 3. Users: Prevented from making accidental or intentional system-wide changes.
Echo.
set /p choice=User Group?
if "%choice%"=="" Echo. & Echo Invalid Entry & Echo. & goto usertype
if %choice% gtr 3 Echo. & Echo Invalid entry & Echo. & goto usertype
if %choice%==1 set usertype=administrators
if %choice%==2 set usertype=power users
if %choice%==3 set usertype=users
Echo.
:Password
set /p pass=Password?
if "%pass%"=="" Echo. & Echo Invalid Entry & Echo. & goto password
Echo.
net user %name% %pass% /add
net localgroup %usertype% %name% /add
Echo.
if "%count%"=="1" CALL Defaultautologon.cmd
set /A count=%count%+1
set /p addmore=Add More Users? Y/n:
if "%addmore%"=="y" GOTO Proceed
Echo.
Echo Finished Adding Users
net accounts /maxpwage:unlimited
PING 1.1.1.1 -n 1 -w 3000 >NUL

useraccounts.cmd

echo REG ADD %path% /v DefaultUserName /d %name% else 
echo REG ADD %path% /v DefaultPassword /d %pass% else

Echo.

set /p autologon=Allow %name% to Autologon? Y/n:
if "%autologon%"=="" Echo. & Echo Invalid Entry goto setdefault
if %autologon%==y Echo REG ADD %path% /v AutoAdminLogon /d 1
GOTO :EOF

Defaultautologon.cmd

Thanks Regan

Edited by reganetal
Link to comment
Share on other sites


Well, the first thing I noticed was that your %path% variable, should not be named path and also points to the incorrect key. The %choice% variable would also be better renamed too!

There is also very little error trapping which could cause many unforseen problems.

Try using something like the example below, which is also attached.

@ECHO OFF&SETLOCAL ENABLEEXTENSIONS&TITLE Account Creation
SET /P TOADD= WOULD YOU LIKE TO ADD ADDITIONAL USERS (Y/N)?
SET TOADD=%TOADD:~0,1%
IF /I '%TOADD% NEQ 'Y ENDLOCAL&GOTO :EOF
SET REGKEY="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
SET CNT1=0
CLS&TITLE Account Name
ECHO/ NOTE&ECHO/ ¯¯¯¯&ECHO/&ECHO/ The First User added will be set as your Default User&ECHO/&ECHO/ It will have Administrator Account privileges&ECHO/
:ADDNAME
FOR %%? IN (NAME TIPE UTYPE PASS) DO (SET %%?=)
ECHO/ PLEASE ENTER YOUR CHOSEN ACCOUNT (LOGIN) NAME
SET /P NAME=
IF "%NAME%" EQU "" ECHO/ Blank Names are not accepted!&&GOTO ADDNAME
ECHO/&ECHO/ THE ACCOUNT NAME YOU HAVE CHOSEN IS&ECHO/&ECHO/ %NAME%&ECHO/&ECHO/ WOULD YOU LIKE TO CHANGE IT (Y/N)?
SET /P ANSR=
SET ANSR=%ANSR:~0,1%
IF /I %ANSR% NEQ N GOTO ADDNAME
NET USER|FIND /I " %NAME% ">NUL 2>&1&&(ECHO/&ECHO/ USER %NAME% ALREADY EXISTS&ECHO/&ECHO/ PLEASE TRY AGAIN&ECHO/&GOTO ADDNAME)
CLS&TITLE Account Type
IF %CNT1% EQU 0 SET UTYPE=Administrator&GOTO ADDPASS
:ADDTYPE
ECHO/ YOU CAN NOW CHOOSE AN ACCOUNT TYPE FOR %NAME%&ECHO/&ECHO/ Account Types&ECHO/ ¯¯¯¯¯¯¯¯¯¯¯¯¯&ECHO/
ECHO/ 1. Computer Administrator (less secure)&ECHO/ Has unrestricted access to the computer&ECHO/
ECHO/ 2. Power User (more secure)&ECHO/ Has only some restricted access to the computer&ECHO/
ECHO/ 3. Normal User (recommended)&ECHO/ Has restricted access to the computer&ECHO/
SET /P TIPE= Please enter your an account type for %NAME% (1/2/3):
SET TIPE=%TIPE:~0,1%
ECHO/%TIPE%|FINDSTR/R "[1-3]">NUL||GOTO ADDTYPE
IF %TIPE% LSS 2 SET UTYPE=Administrator
IF %TIPE% EQU 2 SET UTYPE=Power User
IF %TIPE% GTR 2 SET UTYPE=User
ECHO/&ECHO/ THE ACCOUNT TYPE YOU HAVE CHOSEN IS&ECHO/&ECHO/ %UTYPE%&ECHO/&ECHO/ WOULD YOU LIKE TO CHANGE IT (Y/N)?
SET /P ANSR=
SET ANSR=%ANSR:~0,1%
IF /I %ANSR% NEQ N GOTO ADDTYPE
CLS&TITLE Account Password
:ADDPASS
ECHO/ PLEASE ENTER A PASSWORD FOR %NAME%&ECHO/
ECHO/ NOTE&ECHO/ ¯¯¯¯&ECHO/&ECHO/ Passwords MUST contain a minimum of 6 characters&ECHO/
SET /P PASS=
IF "%PASS%" EQU "" ECHO/&ECHO/ Blank Passwords are not accepted!&ECHO/&GOTO ADDPASS
ECHO/%PASS%|FINDSTR/R "[^a-z,0-9,_,-]">NUL&&(ECHO/&ECHO/ Some of the characters you used were not acceptable!&ECHO/&GOTO ADDPASS)
ECHO/&ECHO/ THE PASSWORD YOU HAVE CHOSEN FOR %NAME% IS&ECHO/&ECHO/ %PASS%&ECHO/&ECHO/ WOULD YOU LIKE TO CHANGE IT (Y/N)?
SET /P ANSR=
SET ANSR=%ANSR:~0,1%
IF /I %ANSR% NEQ N GOTO ADDPASS
SET CNT2=0
:LOOP
SET /A CNT2+=1
CALL SET V=%%PASS:~0,%CNT2%%%
IF "%V%" NEQ "%PASS%" GOTO LOOP
IF %CNT2% LSS 6 ECHO/ Minimum Password length is 6 characters&ECHO/&ECHO/ Please try again!&ECHO/&GOTO ADDPASS
CLS&TITLE Adding Account
NET USER "%NAME%" "%PASS%" /add
NET LOCALGROUP "%UTYPE%s" "%NAME%" /add
IF %CNT1% EQU 0 CALL :DEFLOG
SET /P ADDMORE= WOULD YOU LIKE TO ADD ANOTHER USER (Y/N)?
SET %ADDMORE%=%ADDMORE:~0,1%
IF /I %ADDMORE% EQU Y SET /A CNT1+=1&CLS&GOTO ADDNAME
ECHO/&ECHO/ FINISHED ADDING USERS&ECHO/ ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯&ECHO/
NET ACCOUNTS /maxpwage:unlimited
PING -n 4 127.0.0.1>NUL&ENDLOCAL&GOTO :EOF
:DEFLOG
REG ADD %REGKEY% /V DefaultUserName /D "%NAME%" /F
REG ADD %REGKEY% /V DefaultPassword /D "%PASS%" /F
SET /P AUTLOG= Would you like %NAME%s Account to Logon Automatically (Y/N)?
SET AUTLOG=%AUTLOG:~0,1%
IF /I %AUTLOG% EQU Y REG ADD %REGKEY% /V AutoAdminLogon /D 1 /F
GOTO :EOF

AddUser.zip

Link to comment
Share on other sites

Yzowl,

This is great! I read through the script and there is one thing I just dont understand

CALL SET V=%%PASS:~0,%CNT2%%%

It seems like this part of the code counts how many characters are in %pass%. Am I correct? But I am really unsure how it does this.

ECHO/ NOTE&ECHO/ ¯¯¯¯&ECHO/&ECHO/ The First User added will be set as your Default User&ECHO/&ECHO/ It will have Administrator Account privileges&ECHO/

Also, I noticed you wrote a lot of @echo/ ... is the / some kind of carriage return? And how do you get the upper ____ to make >>> characters? When I type that or paste it into my cmd window I get an underline.

Lastly, maybe I misunderstand the default user part. When I run this script on my computer to test it out, it changes my settings so that "users must enter a user name and password to use this computer" is unchecked. Thats not what I want to do. I want each user to be required to login under their own name. I am using win2k right now so this may work out different on XP I am unsure.

Thanks for all your help.

Regan

Link to comment
Share on other sites

@ reganetal

  • %PASS:~0,1% would expand the PASS variable, and then use only the 1st character, (offset 0), of the expanded result.
    %PASS:~0,2% would expand the PASS variable, and then use only the 1st two characters, of the expanded result.
    etc...

The loop continues until variable V=variable PASS, at this point, CNT2=number of characters

The & character is replacing the 'carriage return'

ECHO/ NOTE&ECHO/ ¯¯¯¯&ECHO/&ECHO/ The First User added will be set as your Default User&ECHO/&ECHO/ It will have Administrator Account privileges&ECHO/
is the same as

ECHO/ NOTE

ECHO/ ¯¯¯¯

ECHO/

ECHO/ The First User added will be set as your Default User

ECHO/

ECHO/ It will have Administrator Account privileges

ECHO/

Finally I have changed no settings other than those you used, they are these lines

  • NET USER "%NAME%" "%PASS%" /add
    NET LOCALGROUP "%UTYPE%s" "%NAME%" /add
    NET ACCOUNTS /maxpwage:unlimited
    REG ADD %REGKEY% /V DefaultUserName /D "%NAME%" /F
    REG ADD %REGKEY% /V DefaultPassword /D "%PASS%" /F
    IF /I %AUTLOG% EQU Y REG ADD %REGKEY% /V AutoAdminLogon /D 1 /F

For testing purposes, just add ECHO/ as shown

ECHO/NET USER "%NAME%" "%PASS%" /add

ECHO/NET LOCALGROUP "%UTYPE%s" "%NAME%" /add

ECHO/NET ACCOUNTS /maxpwage:unlimited

ECHO/REG ADD %REGKEY% /V DefaultUserName /D "%NAME%" /F

ECHO/REG ADD %REGKEY% /V DefaultPassword /D "%PASS%" /F

IF /I %AUTLOG% EQU Y ECHO/REG ADD %REGKEY% /V AutoAdminLogon /D 1 /F

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...