Automating account creation is especially useful in lab environments where you might need to set up multiple accounts frequently. By using a CSV file, you can easily manage and modify the list of accounts to be created. The PowerShell script then makes the process of creating these accounts seamless.
Prepare a CSV File: The CSV file should contain details of the accounts you wish to create. Here’s a sample format:
firstname;lastname;username;department;ou;password
Hugo;Lloris;Hugo.Lloris;Sales;OU=LAB,OU=Utilisateurs,DC=leblogosd,DC=lan;Password1
Lilian;Thuram;Lilian.Thuram;Sales;OU=LAB,OU=Utilisateurs,DC=leblogosd,DC=lan;Password1
Olivier;Giroud;Olivier.Giroud;Sales;OU=LAB,OU=Utilisateurs,DC=leblogosd,DC=lan;Password1
Thierry;Henry;Thierry.Henry;Sales;OU=LAB,OU=Utilisateurs,DC=leblogosd,DC=lan;Password1
Antoine;Griezmann;Antoine.Griezmann;Sales;OU=LAB,OU=Utilisateurs,DC=leblogosd,DC=lan;Password1
Marcel;Desailly;Marcel.Desailly;Sales;OU=LAB,OU=Utilisateurs,DC=leblogosd,DC=lan;Password1
Zinedine;Zidane;Zinedine.Zidane;Sales;OU=LAB,OU=Utilisateurs,DC=leblogosd,DC=lan;Password1
Patrick;Vieira;Patrick.Vieira;Sales;OU=LAB,OU=Utilisateurs,DC=leblogosd,DC=lan;Password1
Didier;Deschamps;Didier.Deschamps;Sales;OU=LAB,OU=Utilisateurs,DC=leblogosd,DC=lan;Password1
Laurent;Blanc;Laurent.Blanc;Sales;OU=LAB,OU=Utilisateurs,DC=leblogosd,DC=lan;Password1
Bixente;Lizarazu;Bixente.Lizarazu;Sales;OU=LAB,OU=Utilisateurs,DC=leblogosd,DC=lan;Password1
Karim;Benzema;Karim.Benzema;Sales;OU=LAB,OU=Utilisateurs,DC=leblogosd,DC=lan;Password1
Raphael;Varane;Raphael.Varane;Sales;OU=LAB,OU=Utilisateurs,DC=leblogosd,DC=lan;Password1
Sylvain;Wiltord;Sylvain.Wiltord;Sales;OU=LAB,OU=Utilisateurs,DC=leblogosd,DC=lan;Password1
Paul;Pogba;Paul.Pogba;Sales;OU=LAB,OU=Utilisateurs,DC=leblogosd,DC=lan;Password1
Fabien;Barthez;Fabien.Barthez;Sales;OU=LAB,OU=Utilisateurs,DC=leblogosd,DC=lan;Password1
William;Gallas;William.Gallas;Sales;OU=LAB,OU=Utilisateurs,DC=leblogosd,DC=lan;Password1
PowerShell Script for Account Creation:
$AD_Users = Import-csv C:\temp\CompteAD.csv -Delimiter ";"
foreach ($User in $AD_Users) {
$User_name = $User.username
$User_Password = $User.password
$First_name = $User.firstname
$Last_name = $User.lastname
$User_Department = $User.department
$User_OU = $User.ou
write-host $User_name
if (Get-ADUser -F {SamAccountName -eq $User_name}) {
Write-Warning "A user $User_name has already existed in Active Directory."
} else {
New-ADUser -SamAccountName $User_name -UserPrincipalName "$User_name@leblogosd.lan" -Name "$First_name $Last_name" -GivenName $First_name -Surname $Last_name -Enabled $True -ChangePasswordAtLogon $False -DisplayName "$Last_name, $First_name" -Department $User_Department -EmailAddress "$User_name@leblogosd.lan" -PasswordNeverExpires $true -Path $User_OU -AccountPassword (convertto-securestring $User_Password -AsPlainText -Force)
}
}
This script checks if the user already exists in Active Directory. If not, it creates a new user with the specified details.
0 Comments