# Bulk Importing users into Domain

Here in this section we will learn how to add users in bulk into the domain controller using windows scripts and the data from the .csv files.

***

## Creating the .csv file :&#x20;

We will create the .csv file and fill the data into it in the following syntax:&#x20;

{% file src="/files/Ja32CbGXBcGuMYdK2xok" %}

<figure><img src="/files/2Esrnlw2ISWweKXJT470" alt=""><figcaption></figcaption></figure>

Now we will Import the data inside the powershell using the following command :&#x20;

```powershell
Import-Csv <path_to_data.csv> | Format-Table 
```

The output should look like this :&#x20;

<figure><img src="/files/X1t1Bu5aloRUv68IPEvX" alt=""><figcaption></figcaption></figure>

#### Importing Script :&#x20;

After making the necessary changes in the following script given here we can proceed adding the users in our active directory domain.

{% code overflow="wrap" %}

```powershell
# Import active directory module for running AD cmdlets
Import-Module ActiveDirectory
  
# Store the data from MultiUser.csv in the $ADUsers variable
$ADUsers = Import-Csv data.csv 

# Define UPN, replace hg.local = Your-UPN
$UPN = "rookie.local"

# Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers) {

    #Read user data from each field in each row and assign the data to a variable as below
    $username = $User.username
    $password = $User.password
    $firstname = $User.firstname
    $lastname = $User.lastname
    $initials = $User.initials
    $OU = $User.ou
    $email = $User.email
    $streetaddress = $User.streetaddress
    $city = $User.city
    $zipcode = $User.zipcode
    $state = $User.state
    $country = $User.country
    $telephone = $User.telephone
    $jobtitle = $User.jobtitle
    $company = $User.company
    $department = $User.department

    # Check to see if the user already exists in AD
    if (Get-ADUser -F { SamAccountName -eq $username }) {
        
        # If user does exist, give a warning
        Write-Warning "A user account with username $username already exists in Active Directory."
    }
    else {

        # User does not exist then proceed to create the new user account
        # Account will be created in the OU provided by the $OU variable read from the CSV file
        New-ADUser `
            -SamAccountName $username `
            -UserPrincipalName "$username@$UPN" `
            -Name "$firstname $lastname" `
            -GivenName $firstname `
            -Surname $lastname `
            -Initials $initials `
            -Enabled $True `
            -DisplayName "$lastname, $firstname" `
            -Path $OU `
            -City $city `
            -PostalCode $zipcode `
            -Country $country `
            -Company $company `
            -State $state `
            -StreetAddress $streetaddress `
            -OfficePhone $telephone `
            -EmailAddress $email `
            -Title $jobtitle `
            -Department $department `
            -AccountPassword (ConvertTo-secureString $password -AsPlainText -Force) -ChangePasswordAtLogon $True

        # If user is created, show message.
        Write-Host "The user account $username is created." -ForegroundColor Cyan
    }
}

Read-Host -Prompt "Press Enter to exit"
```

{% endcode %}

#### Running the Powershell script:&#x20;

We will run the following script in the powershell ISE application in the windows server ISE shell

<figure><img src="/files/bLWFQpAWnCNSIieSU4v1" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/ZyZIFs75zcWmyJXv3hlP" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/LYEqph5IsYIkRrISVRIm" alt=""><figcaption></figcaption></figure>

Select the whole code and click on the run script button so that the script can be executed

<figure><img src="/files/EuDsznStutIzLPuvacqy" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/HnV4H2EyiH1sxZbExhqk" alt=""><figcaption></figcaption></figure>

Once the script will run the output should look like this and the users will be reflected in the Active Directory Users & Computers section.

<figure><img src="/files/MyqmYxN5jMHS7BsJiZKa" alt=""><figcaption></figcaption></figure>

This is how we can bulk import users into the domain and make the import task easier when adding a lot of users or migrating users from the other Active directory domains.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ghoulsec.gitbook.io/ghoulsec-vault/server-are-fun/servers/windows-sever/bulk-importing-users-into-domain.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
