Windows batch script that can be used to automate the process of adding a user to Active Directory:
@echo off
REM Set the variables for the user's name, password, and OU
set userName=JohnDoe
set userPassword=P@ssw0rd
set userOU=CN=Users,DC=example,DC=com
REM Create the user account in Active Directory
dsadd user "CN=%userName%,%userOU%" -pwd %userPassword% -disabled no
REM Set the user's password to never expire
dsmod user "CN=%userName%,%userOU%" -pwexpire no
REM Add the user to the "Domain Users" group
dsmod group "CN=Domain Users,CN=Users,DC=example,DC=com" -addmbr "CN=%userName%,%userOU%"
This script uses the dsadd
and dsmod
commands to create a new user account in Active Directory and add the user to the “Domain Users” group. It sets the user’s name, password, and organizational unit (OU) using variables at the beginning of the script, and then uses these variables to create the user account and set the password to never expire.
To use this script, you would need to replace the values of the userName
, userPassword
, and userOU
variables with the appropriate values for the user you want to add. You would also need to modify the userOU
variable to specify the correct organizational unit for the user in your Active Directory environment.
Once you have customized the script, you can run it from the command line by navigating to the directory where the script is saved and typing batchScriptName.bat
, where batchScriptName
is the name of the script file. This will execute the script and add the user to Active Directory. You can also use this script as a starting point for creating your own batch scripts for managing users and other objects in Active Directory.