help with a batch file creating users

November 29, 2011 at 14:42:07
Specs: Windows 7
Hi I am very new to batch file writing and was wondering if anyone could give me some help on this problem.....the goal is to create a batch file that will prompt the user from within the batch file to input a first name,last name, and username. I then want to add them as a local user and set their password to 123456 and add them to a local group called "testgroup". I also want to create a home folder and share the directory and set a home directory parameter for the user whcih points to that folder. I need additional usernames to be entered as well.....this is for windows appreciate any help!

See More: help with a batch file creating users

Report •

November 29, 2011 at 15:59:26
Do you mean create a Windows user or a User within the batchfile?
Creating inside Batchfile
To prompt user input use;

Echo Enter First name
Set /p "firstname=>"
Echo Enter Last name
Set /p "lastname=>"
Echo Enter Username
Set /p "username=>"

If you want to use the username as a login you would need a login menu with an IF. And the same for password too.

IF %username% == mand0828 Goto :mand0828
IF %username% == Loq Goto :Loq
Echo Username doesn't exist.

Then you would have your Password set up at your block of code like.

Echo Enter Password
SET /p "password=>"
IF %password% == 123456 Goto :USERmand0828
Echo Password wrong!
Goto :start


Under :USERmand0828 you would put whatever else you want. That's the basic idea of username / password. Although I have to say that this way is very unsecure without a lot of coding to protect it. So it would be unwise to do it this way.
That's if you want it inside a batch file anyway.

If you're still new to it all, it would be best to mess around with the SET command for variables and the IF statements.
I hope this helps a little.

Report •

November 30, 2011 at 11:27:23
This ended up getting somewhat complicated - please let me know if I can help explain any particular portion

@ echo off
:: Prompts
set /p first=First Name: 
set /p last=Last Name: 
set /p uname=User Name: 

:: Verify Unique Username
net user | findstr /i /C:"%uname%" >nul
if %errorlevel% equ 0 goto NonUnique

goto confirm

Echo The username you entered is not unique, please enter a new username
set /p uname=User Name:
goto verify 

:: Confirm
Echo A new user with the following details will be created:
Echo. Full Name: %first% %last%
Echo. Username: %uname%
set /p confirm=Would you like to re-enter these details (y/n): 
if /I %confirm% equ y goto CreateUserPrompts
if /I %confirm% equ yes goto CreateUserPrompts

:: YOUR INPUT HERE (you can use %first% %last% and %uname% variables to assure unique folder)
set HomeFolderPath=%systemdrive%\yourpathhere

:: Create Home Folder
mkdir "%HomeFolderPath%"

:: Share Home Folder
net share NameOfShareHere="%HomeFolderPath%"

:: Create User
net user %uname% 123456 /ADD /ACTIVE:YES /PASSWORDCHG:NO /EXPIRES:NEVER /FULLNAME:"%first% %last%" 


:: Add User to Group
net localgroup "testgroup" %uname% /add
Echo Script Complete - Press any key to exit
Pause >nul

Report •
Related Solutions

Ask Question