Créer des utilisateurs AD facilement avec PowerShell

Un script PowerShell ultra simple que j'utilise pour créer des utilisateurs Active Directory. Nous allons ici créer trois utilisateurs AD ("Bruce Wayne","Alfred Pennyworth","Selina KYLE") avec le même mot de passe : "ComplexP@ss908".

Code v1

Script PowerShell

###########################
# 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 corrections 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
}

Exécution du script

Les différentes lignes montrent les étapes pour le formatage des noms utilisateurs. Le but étant d'arriver à un format du type Prénom NOM et ce indépendamment de la casse des noms fournis en entrée.

PowerShell AD users Output

Résultat

AD users Output
AD user Properties

Code v2

Nouvelle version qui permet de générer automatiquement un mot de passe de 8 caractères, la liste des utilisateurs comporte les noms, prénoms et identifiants.

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 :