Release date: December 22nd 2022
Welcome to my Microsoft Tips & Tricks section. I recently experienced a complete hardware crash in my old lab, and had to do a complete rebuild. Although this was sad, it gave me the opportunity to do it by scripting this time. This involved recreating the Admin Users, as shown below.
The script below uses a csv file with the following data:
- FirstName
- Lastname
- DisplayName
- Username
- Password
- OU
- Description
Next, using the script below, I easily recreated the Admin Users in AD, however, I did have to manually change the passwords afterwards. NOTE: The user I ran this script with had Domain Admin permissions, otherwise this wouldn’t have worked.
Import-Module ActiveDirectory
$ADUsers = Import-Csv "<Path to csv-file>.csv" -Delimiter ";"
$UPN = "ad.admin.frelab.net"
foreach ($User in $ADUsers) {
$username = $User.username
$password = $User.password
$firstname = $User.firstname
$lastname = $User.lastname
$displayname = $User.displayname
$description = $User.description
$OU = $User.ou
if (Get-ADUser -F { SamAccountName -eq $username }) {
Write-Warning "The user $username already exists in AD."
}
else {
New-ADUser `
-SamAccountName $username `
-UserPrincipalName "$username@$UPN" `
-Name "$firstname $lastname" `
-GivenName $firstname `
-Surname $lastname `
-Description $description `
-Enabled $True `
-DisplayName "$lastname, $firstname" `
-Path $OU `
-AccountPassword (ConvertTo-secureString $password -AsPlainText -Force)
-ChangePasswordAtLogon $True
# -PasswordNeverExpires $True
Write-Host "The user account $username is created." -ForegroundColor Cyan
}
}
Read-Host -Prompt "Press Enter to exit"
Now that I had created the Admin Users and Groups, I could proceed with putting the specific admin users into their corresponding admin groups, which I also did by using a script, detailed here: Add Admin Users to Admin Groups based on csv-file
Big thanks to: Ali Tajran’s: Create Active Directory Users from CSV with PowerShell
Microsoft Tips & Tricks section
Disclaimer: Every tips/tricks/posting I have published here, is tried and tested in different it-solutions. It is not guaranteed to work everywhere, but is meant as a tip for other users out there. Remember, Google is your friend and don’t be afraid to steal with pride! Feel free to comment below as needed.