rss logo

Create AD Users with PowerShell

A very simple PowerShell script to create Active Directory users. We will create three AD users ("Bruce Wayne","Alfred Pennyworth","Selina KYLE") with the same password : "ComplexP@ss908".

Code v1

PowerShell Script

###########################
# author : shebangthedolphins.net
# version : 1.1
# date : 2020.09
# role : Create AD users from a list
# other : Tested on Windows 2016 Server
# updates :
#       - 1.X (2018/06) : First Version
#       - 1.1 (2020/09) : Small updates with Write-Host part

$users = New-Object System.Collections.ArrayList
[void] $users.AddRange( ("Bruce Wayne","Alfred Pennyworth","Selina KYLE"))
$password = "ComplexP@ss908!"
$addomain = "shebangthedolphins.net"

Foreach ($user in $users)
{
    Write-Host "My user : $user"
    $pos = $user.IndexOf(" ")
    $first_name = $user.Substring(0, $pos).ToLower()
    $name = $user.Substring($pos+1).ToLower()

    $first_name_ = "$($first_name.substring(0,1).ToUpper())$($first_name.Substring(1))"
    $name_ = "$($name.substring(0,1).ToUpper())$($name.Substring(1))"
    $first_name__ = $first_name.ToUpper()
    $name__ = $name.ToUpper()

    Write-Host "first_name : $first_name"
    Write-Host "name : $name"

    Write-Host "First_name : $first_name_"
    Write-Host "Name : $name_"

    Write-Host "FIRSTNAME : $first_name__"
    Write-Host "NAME : $name__"
    Write-Host "Account : $($first_name).$($name)@$($addomain)"

    New-ADUser -Name "$($first_name_) $($name__)" -GivenName $first_name_ -Surname $name__ -SamAccountName "$($first_name).$($name)" -UserPrincipalName "$($first_name).$($name)@$($addomain)" -AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force) -PassThru | Enable-ADAccount
}

Output

PowerShell AD users Output

Result

AD users Output
AD user Properties

Code v2

New version, it allows to automatically generate a 8 characters user password, users list is now in this format : last name, first name et AD login.

PowerShell Script

###########################
# author : shebangthedolphins.net
# version : 2.0
# date : 2020.10
# role : Create AD users from a list
# other : Tested on Windows 2016 Server
# updates :
#       - 1.X (2018/06) : First Version
#       - 1.1 (2020/09) : Small corrections with Write-Host part
#       - 2.0 (2020/10) : Auto generate password

#Needed for auto password
Add-Type -AssemblyName "System.Web" -ErrorAction Stop

#Set your domain here
$addomain = "shebangthedolphins.net"

$users = New-Object System.Collections.ArrayList
#user list : "LASTNAME FIRSTNAME ad.login"
[void] $users.AddRange(( "CARTMAN Eric e.cartman", "Broflovski Kyle k.Broflovski", "Marsh Randy r.marsh", "Stotch Butters b.Stotch" ))
Foreach ($user in $users)
{
    $name = $user.split(" ")[0].ToLower()
    $first_name = $user.split(" ")[1].ToLower()
    $login = $user.split(" ")[2].ToLower()

    $first_name_ = "$($first_name.substring(0,1).ToUpper())$($first_name.Substring(1))"
    $name_ = "$($name.substring(0,1).ToUpper())$($name.Substring(1))"
    $first_name__ = $first_name.ToUpper()
    $name__ = $name.ToUpper()

    $password = [System.Web.Security.Membership]::GeneratePassword(8,1)
    Write-Host "Name : $first_name_ $name__ `nUser : $login  `nPassword : $password"
    Write-Host "-----------------------"
    
    New-ADUser -Name "$($first_name_) $($name__)" -GivenName $first_name_ -Surname $name__ -SamAccountName "$($login)" -UserPrincipalName "$($login)@$($addomain)" -AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force) -PassThru | Enable-ADAccount
}

Output

PowerShell AD users create Output
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address