I need to create a batch file using the "net user" command. I'm familiar with creating accounts using the "net user" command, but I need to create a batch file that asks the operator to enter the user name to be created, then run the command using the "net user" interface.
I'm more or less familiar with the commands needed to do this, I'm just not exactly sure how to order them all and execute them in the instance of using the "net user" interface.
Any tips would be great, I'm not asking for someone to do my dirty work and write the batch file for me, I'm just asking for someone to point me in the right direction.
Any help or tips would be great.
Batch file for creating new user account
Moderator: DosItHelp
Re: Batch file for creating new user account
Since you know how to use the NET command all you need then is a prompt to enter in the username and password. You can use the SET /P command for that.
Code: Select all
SET /P user=Please Enter the Username:
SET /P password=Please Enter the Password:
net user %user% %password% /add
Re: Batch file for creating new user account
You could even forget about using net.exe and go for a powershell script, (suggested for no other reason but to mask the input password).
Example with no built in checking/safety for the input
Example with no built in checking/safety for the input
Code: Select all
$desc = 'local admin account created by interactive script'
$username = Read-Host -Prompt "Enter a User Name"
$password = Read-Host -Prompt "Enter a password for $username" -AsSecureString
$decodedpassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
$computername = [System.Environment]::MachineName
$computer = [ADSI]"WinNT://$computername,computer"
$user = $computer.Create("user", $username)
$user.SetPassword($decodedpassword)
$user.SetInfo()
$user.description = $desc
$user.SetInfo()
$user.UserFlags = 65536
$user.SetInfo()
$group = [ADSI]("WinNT://$computername/administrators,group")
$group.add("WinNT://$username,user")
Re: Batch file for creating new user account
Compo wrote:You could even forget about using net.exe and go for a powershell script, (suggested for no other reason but to mask the input password).
I believe we have a way to mask the password in batch as well. Would need to search for the thread but I had mentioned that we should add it to the main website's function library.
-
- Expert
- Posts: 1167
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Batch file for creating new user account
We do and it's beautiful.
viewtopic.php?p=33538#p33538
Code: Select all
@echo off
setlocal enableextensions disabledelayedexpansion
rem Call the subroutine to get the password
call :getPassword password
rem Echo what the function returns
if defined password (
echo You have typed [%password%]
) else (
echo You have typed nothing
)
rem End of the process
endlocal
exit /b
rem Subroutine to get the password
:getPassword returnVar
setlocal enableextensions disabledelayedexpansion
set "_password="
rem We need a backspace to handle character removal
for /f %%a in ('"prompt;$H&for %%b in (0) do rem"') do set "BS=%%a"
rem Prompt the user
set /p "=password ?:" <nul
:keyLoop
rem retrieve a keypress
set "key="
for /f "delims=" %%a in ('xcopy /l /w "%~f0" "%~f0" 2^>nul') do if not defined key set "key=%%a"
set "key=%key:~-1%"
rem handle the keypress
rem if No keypress (enter), then exit
rem if backspace, remove character from password and console
rem else add character to password and go ask for next one
if defined key (
if "%key%"=="%BS%" (
if defined _password (
set "_password=%_password:~0,-1%"
setlocal enabledelayedexpansion & set /p "=!BS! !BS!"<nul & endlocal
)
) else (
set "_password=%_password%%key%"
rem remove the * for UNIX-style password masking
set /p "=*"<nul
)
goto :keyLoop
)
echo(
rem return password to caller
if defined _password ( set "exitCode=0" ) else ( set "exitCode=1" )
endlocal & set "%~1=%_password%" & exit /b %exitCode%
viewtopic.php?p=33538#p33538
Re: Batch file for creating new user account
Thanks,
And I think Carlos' script has been improved over the last few years as well.
And I think Carlos' script has been improved over the last few years as well.