
If you need to create many new user accounts in a domain at once, it is extremely inefficient to create them all manually from the graphical console Active Directory Users and Computers (ADUC). In this article, we will take a look at a simple PowerShell script that allows you to import user data from a CSV/XLS file and create accounts for them in the Active Directory domain.
First of all, create a NewUser.xlsx file in Excel with the following header structure:
FullName;sn;givenName;company;department;title;telephoneNumber;City;sAMAccountName;Password
Fill the Excel file with the data of all users that you want to create in Active Directory (usually this data is provided from the personnel accounting system).
Export the Excel file to CSV format with commas as separators (File > Save as > File type: CSV, File name: new_as_users.csv). If you want to use “;” as a separator, you need to add the following argument to the Import-CSV cmdlet -delimiter “;”.
To create new users in the domain, we will use the New-ADUser cmdlet from the Active Directory for Windows PowerShell module, so before running the PowerShell script, make sure that this module is installed on the server/computer from which you are performing users import.
Create an import_ad_users.ps1 file with the following PowerShell code (change the name of your domain and the Active Directory Organizational Unit (OU) in which you want to create users):
Import-Module ActiveDirectory $Domain="@theitbros.cpm" $UserOu="OU=Users,OU=UK,DC=theitbros,DC=com" $NewUsersList=Import-CSV "C:\PS\new_as_users.csv" ForEach ($User in $NewUsersList) { $FullName=$User.FullName $Company=$User.company $Department=$User.department $Description=$User.description $givenName=$User.givenName $title=$User.title $City=$User.City $telephoneNumber=$User.telephoneNumber $sAMAccountName=$User.sAMAccountName $sn=$User.sn $userPrincipalName=$User.sAMAccountName+$Domain $userPassword=$User.Password $expire=$null New-ADUser -PassThru -Path $UserOu -Enabled $True -ChangePasswordAtLogon $True -AccountPassword (ConvertTo-SecureString $userPassword -AsPlainText -Force) -CannotChangePassword $False -City $City -Company $Company -Department $Department –title $title –OfficePhone $telephoneNumber -DisplayName $FullName -GivenName $givenName -Name $FullName -SamAccountName $sAMAccountName -Surname $sn -UserPrincipalName $userPrincipalName }
Note. Options-ChangePasswordAtLogon $True requires changing the user password at the first login, -CannotChangePassword $False – allows the user to change passwords by himself. If you create service accounts, you can specify -ChangePasswordAtLogon $False, -CannotChangePassword $True.
Run the script from the PowerShell command prompt, then open the ADUC console and make sure that new users appeared in the specified OU.
As you can see, this PowerShell script allows the mass import of users into Active Directory in a few minutes. You can remove or add any user attributes to the script and CSV/Excel file from AD. A complete list of available user attributes in your domain schema can be displayed using the following cmdlet:
Get-ADUser –identity administrator –filter * -properties *|fl
The post Import Users Into Active Directory From CSV appeared first on TheITBros.