Situatie
Solutie
Another option for creating users in AD is to import them from a CSV file. This option is great when you have a list of users with predefined personal details such as:
- FirstName
- LastName
- Username
- Department
- Password
- OU
The CSV file must be in UTF8 encoding and contain contact data that looks like this:
The following script will create enabled user objects for any users in the CSV that don’t already have accounts in AD. The “Reset password at the next logon” option will be enabled for the new accounts, so you can use your default password:
#Enter a path to your import CSV file $ADUsers = Import-csv C:\scripts\newusers.csv foreach ($User in $ADUsers) { $Username = $User.username $Password = $User.password $Firstname = $User.firstname $Lastname = $User.lastname $Department = $User.department $OU = $User.ou #Check if the user account already exists in AD if (Get-ADUser -F {SamAccountName -eq $Username}) { #If user does exist, output a warning message Write-Warning "A user account $Username has already exist in Active Directory." } else { #If a user does not exist then create a new user account #Account will be created in the OU listed in the $OU variable in the CSV file; don’t forget to change the domain name in the"-UserPrincipalName" variable New-ADUser ` -SamAccountName $Username ` -UserPrincipalName "$Username@yourdomain.com" ` -Name "$Firstname $Lastname" ` -GivenName $Firstname ` -Surname $Lastname ` -Enabled $True ` -ChangePasswordAtLogon $True ` -DisplayName "$Lastname, $Firstname" ` -Department $Department ` -Path $OU ` -AccountPassword (convertto-securestring $Password -AsPlainText -Force) } }
After script execution, we have two new users, Edward Franklin and Bill Jackson, in our Active Directory domain:
Let’s take a look at their details by running Get-ADUser cmdlet again:
Get-ADUser E.Franklin -Properties CanonicalName, Enabled, GivenName, Surname, Name, UserPrincipalName, samAccountName, whenCreated, PasswordLastSet | Select CanonicalName, Enabled, GivenName, Surname, Name, UserPrincipalName, samAccountName, whenCreated, PasswordLastSet
Leave A Comment?