Maica

How to Mass Create Users in Windows Server

Remember that this Post is based on Windows Server 2021, and in this case it will not work for other versions.

Before starting, I would like to thank you for your requests, because they help me to know what topics interest you the most so that I can offer you useful, practical information adapted to your needs.

We will go step by step to facilitate understanding.

Step 1: Create *.XLSX file

The first thing we need is a *.XLSX file with all the data of the users to be registered in AD (Active Directory). We can do this with Excel, in which you can create, for example, the fields “ACCOUNT, NAME, SURNAME, ADDRESS, DESCRIPTION”.

The first Line/Row of this Excel is the one that will be used as the header of each column to link with the Active Directory fields . The file will first be saved as USERS.xlsx. It should be as follows:

Next, we will save the same file with the name of USERS.csv . Remember that when saving, you have to select the format “ CSV (comma delimited) (*.csv) ”. Check it on the next picture

This will create a file with the extension .CSV. Since we had already created the USUARIOS.xlsx file at the beginning, there will now be two files with the same name but different extensions:

Step 2: Script Creation

Before starting with the creation of the Script, we will carry out an important verification. We have to check that the USUARIOS.CVS file is delimited by commas so that it can be read correctly from the Script.

To do this we are going to open the file from Notepad. As you can see in the image below, the fields are separated by “;”. This is incorrect, and therefore we have to change it to “,” Pay attention to these little details!

Once modified, it should be like this:

Notice how we’ve replaced the erroneous semicolons with ‘,’

Now we only have to save the modifications and close the Notepad.

Once this verification is done, it is time to create the Script . To do this, as a previous step, we will copy the two files “USUARIOS.csv and USUARIOS.xlsx” to our Domain Controller or DC, which is where we will execute it from.

Once these steps are done, we go to the Script part. This will be called ADDUSERS.PS1 and will have the following content (The Yellow Lines are Explanatory text, you can ignore them)

We will start by importing the Active Directory Module

Import-Module ActiveDirectory

Here we will type the Path where the *.CSV File that we have created is located

[String]$Ruta = Read-Host «Path where the file USERS.csv is located»

The following statement indicates that we are going to create an Organizational Unit called “Usuarios-Moviles” in the Current Domain within Active Directory

$ ou = »OU = Mobile-Users» + «,» + (Get-ADDomain) .DistinguishedName

The following Line Checks that the Organizational Unit does not Exist, if so it will create the Organizational Unit, otherwise it will continue with said Script

If(-Not(Get-ADOrganizationalUnit -Filter {Name -eq «Usuarios-Moviles»})){New-ADOrganizationalUnit «Usuarios-Moviles» -Path (Get-DDomain).DistinguishedName}

$dominio=(Get-ADDomain).DNSRoot

We import the CSV File and later it will create the Users within the Current Domain and the previously created OU. Here we also link each field of EXCEL with the real one of Active Directory

Import-Csv -Path $Ruta | foreach-object {$UPN = $_.CUENTA + «@» + «$dominio»

New-ADUser -SamAccountName $_.CUENTA -UserPrincipalName $UPN -Name $_.NOMBRE -DisplayName $_.NOMBRE -SurName $_.APELLIDOS -GivenName $_.NOMBRES -Description $_.DESCRIPCION -Office $_.OFICINA -OfficePhone $_.TELEFONO -EmailAddress $_.EMAIL -Title $_.TITULO -Department $_.DEPARTAMENTO -Company $_.COMPANIA -City $_.CIUDAD -State $_.ESTADO -AccountPassword (ConvertTo-SecureString $_.Clave -AsPlainText -force) -Path $ou -Enabled $true -ChangePasswordAtLogon $true -Verbose}

Step 3: Run the Script

Now we will execute the Script within PowerShell with Administrator permissions

Remember that you should have previously copied the .CSV and .PS1 file into the Domain Controller where the Script will be executed. In my case I copied them to drive C:\>

In a few seconds we will have created all the users within AD

PS c:\ .\ADDUSERS.PS1

If we enter the “Active Directory Users and Computers” console, we will see how there is a new Organizational Unit created “Mobile-Users”, and within it all our created users.

Related Articles

Leave a Reply

Your email address will not be published.

CAPTCHA


Check Also
Close
Back to top button