Create domain users in AD with Powershell

When a large amount of users – with quite similar roles indeed – should be created something must be done to automate the task.

In this case reading from a txt file containing user data is very handy. And we can fill almost any user properties for every account.

# ———————————————————
# Imports a TXT File with following fields separated by ,
# FirstName,LastName,Email,Rol
# ———————————————————

# Domain Details
$DomainSuffix = “cantoso.com”
$TargetDomain = “,DC=cantoso,DC=com”
$TargetUserOU=”OU=Location,OU=Usuarios,OU=Company”
$DefaultPassword = “Ws34¿?!X3”

# Users Details
$TestGroup = “CN=G_Machotes,OU=Groups,OU=Company” + $TargetDomain

$InputFile=”CantosoUserList.txt”
$TSHome = “\\Fileserver01\home\”
$TSProfile = “\\Fileserver01\tsprofile\”
$TSHomeDrive = “H:”
$LoginScriptFileName = “Loginscript.cmd”

# Office Details
$WWW= “http://cantoso.com”
$NumericUserAccountDigit=600
$PostalCode = “3200”
$City = “La Botija”
$Province = “ValdePetarda”
$Address = “Paseo del Torpedo, 8”
$Country = “Spain”
$TelephoneNumber =”+3434333222″

$ADs_UF_NORMAL_ACCOUNT = 512 ## enables account and sets password required

#Connect to Domain and User Container
$LdapStr = “LDAP://” + $TargetUserOU + $TargetDomain
$Target = [ADSI] $LdapStr

Import-Csv $InputFile | ForEach {

$Username= $_.FirstName + ” ” + $_.LastName

#Create UserName under Specific OU
$NewUser = $Target.Create(“User”,”CN=” + $UserName)
$NewUser.SetInfo()

#Create UserID and Other Properties

$UserID = “ES000″ + $NumericUserAccountDigit
$NewUser.SamAccountName = $UserID.ToString()
$NewUser.GivenName = $_.FirstName
$NewUser.Sn = $_.LastName
$NewUser.DisplayName = $_.FirstName + ” ” + $_.LastName
$NewUser.UserPrincipalName = $UserID + “@” + $DomainSuffix
$NewUser.Description = $_.Rol
$NewUser.Mail = $_.Email.tostring()
$NewUser.physicalDeliveryOfficeName = $Province.Tostring()
$NewUser.TelephoneNumber = $TelephoneNumber
$NewUser.wWWHomePage = $WWW.ToString()
$NewUser.streetAddress = $Address.ToString()
$NewUser.st= $Province.ToString()
$NewUser.l = $City.ToString()
$newUser.c = “ES”
$NewUser.co = $Country.ToString()
$NewUser.PostalCode = $PostalCode.ToString()
$NewUser.PostOfficeBox = $PostalCode.ToString()

$NewUser.SetInfo()

#Password

$NewUser.SetPassword($DefaultPassword.ToString())

#Terminal Services Properties
$Newuser.PSBase.InvokeSet(“AccountDisabled”,”False”)
$Newuser.PSBase.InvokeSet(“TerminalServicesHomeDirectory”,$TSHome + $UserID)
$Newuser.PSBase.InvokeSet(“TerminalServicesProfilePath”,$TSProfile + $UserID)
$Newuser.PSBase.InvokeSet(“TerminalServicesHomeDrive”,$TSHomeDrive)

#Profile
$Newuser.PSBase.InvokeSet(“LoginScript”,$LoginScriptFileName)

$NewUser.pwdLastSet = 0
$NewUser.SetInfo()

# Add Users to the following Groups

$TargetGroup = [ADSI]”LDAP://$TestGroup”
$TargetGroup.member.Add([string]$NewUser.distinguishedName)
$TargetGroup.SetInfo()

Write-Host “Account created on ” $TargetDomain ” for user ” $NewUser.DisplayName

$NumericUserAccountDigit = $NumericUserAccountDigit + 1

}

Advertisements

One thought on “Create domain users in AD with Powershell”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s