With the power and combination of Excel, CSV, and PowerShell you can easily create thousands of users within minutes. There are GUI tools like Active Directory Users and Computers and Active Directory Administrative Center which can be used to create user accounts and other AD objects. Imagine creating 50 user accounts with Active Directory Users and Computers. This method would take lot of time right? With PowerShell, you can automate this process. In this article, I will show steps to create bulk user accounts from Excel CSV file in Server 2008 R2.
Create Bulk User Accounts from Excel CSV file in Server 2008 R2
CSV (Comma Separated Values) files contains values that are aligned in column and separated by comma. You don’t need Excel to create CSV file, you can simple use Notepad to create CSV file. But using Excel, you life is much easier as you can use power of Excel to populate various user attributes. First, let’s take an example of creating single user account using PowerShell cmdlet. New-AdUser cmdlet is used to create new user object in Active Directory as shown below.
PS C:\Users\Administrator> New-ADUser -Name "Bipin Giri" -SamAccountName BGiri -UserPrincipalName bgiri@mustbegeek.com -GivenName Bipin -Surname Giri -DisplayName "Bipin Giri" -AccountPassword (ConvertTo-SecureString p@ssw0rd! -AsPlainText -Force) -ChangePasswordAtLogon $true -Enabled $True -Path "ou=Marketing, ou=MBG-User-Accounts, dc=mustbegeek, dc=com"
The cmdlet above, creates new user account named Bipin Giri in Marketing OU. Now, let’s create user accounts in bulk from CSV file. The CSV needs to have all the required attributes, as shown below. You can create the file in Excel and save it as CSV.
Now copy the CSV file to Domain Controller. CSV file looks like this when open in Notepad,
Here is the script to create new user accounts. The script simply imports CSV file and runs New-ADUser cmdlet for each rows of user from the CSV file.
$UserAccounts = Import-CSV -Path C:\Users\Administrator.MUSTBEGEEK\Desktop\Users.csv Import-Module ActiveDirectory Foreach ($user in $UserAccounts) { $FullName = $user.FullName $GivenName = $user.FirstName $Surname = $user.Lastname $SamAccountName = $user.SamAccountName $UPN = $user.UPN $City = $user.City $Department = $user.department $OU = "ou=$Department, ou=MBG-User-Accounts, dc=mustbegeek, dc=com" New-ADUser -Name "$FullName" -Enabled $True -GivenName "$GivenName" -Surname "$Surname" -AccountPassword (convertTo-SecureString $user.Password -AsPlainText -Force) -ChangePasswordAtLogon $True -SamAccountName "$SamAccountName" -UserPrincipalName "$UPN" -City "$City" -Department "$Department" -Path $OU }
To run the above script, copy and paste the script in Notepad and save it as .ps1.
Before you can run the PowerShell cmdlet, make sure RemoteSigned policy is set to unrestricted,
PS C:\Users\Administrator\Desktop> Set-ExecutionPolicy -ExecutionPolicy Unrestricted Execution Policy Change The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic. Do you want to change the execution policy? [Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): Y
Now, run PowerShell as Administrator and run the script,
PS C:\Users\Administrator\Desktop> .\New-User.ps1
After running the script, the user accounts are created successfully in Active Directory as shown below, as you can see in the CSV file above, we had two users in the Finance department/OU.
You can also view the information of user account from PowerShell using Get-ADUser cmdlet,
PS C:\Users\Administrator\Desktop> Get-ADUser AJones DistinguishedName : CN=Allan Jones,OU=Finance,OU=MBG-User-Accounts,DC=mustbegeek,DC=com Enabled : True GivenName : Allan Name : Allan Jones ObjectClass : user ObjectGUID : 820dc652-aead-487a-a4b6-7419188d6012 SamAccountName : AJones SID : S-1-5-21-2724326554-3306218706-2452827310-1120 Surname : Jones UserPrincipalName : AJones@mustbegeek.com
In this way you can create bulk user accounts from Excel CSV file in Server 2008 R2.
You may also like -
Latest posts by Bipin (see all)
- Install Exchange 2019 in Windows Server 2019 - November 28, 2020
- Why Backup your Microsoft Office 365 - November 27, 2020
- What’s New in VMware vSphere 7 - September 18, 2020