Istanbul/Turkey

Creating Multiple AD Users From CSV File

Updated on 19-10-2021

 

This is the structure of CSV file I am using.

 

 

It imports the csv file and creates a user for each entry. 

Code Example 1:

Import-Csv -Path "C:\scripts\NewUsers.csv" | ForEach-Object{
#$_ refers to the current item in the pipeline
$UPN = $_."LogonUsername" + "@test.local"
New-ADUser -Name $_.Name`
            -SamAccountName $_.LogonUsername`
            -AccountPassword (ConvertTo-SecureString $_.Password -AsPlainText -Force)`
            -Givenname $_.FirstName`
            -Surname $_.LastName`
            -Emailaddress $_.Email`
            -Department $_.Department`
            -displayname $_.Name`
            -enabled $true -passwordneverexpires $true `
            -UserPrincipalName $UPN
}

 

Code Example 2: This example is almost the same but here, after each user is created it adds the user to a AD group by according to the users' department. Then it moves the users to their corresponding OUs according to their department. 

Import-Csv -Path "C:\scripts\NewUsers.csv" | ForEach-Object{
#$_ refers to the current item in the pipeline
$UPN = $_."LogonUsername" + "@test.local"
New-ADUser -Name $_.Name`
            -SamAccountName $_.LogonUsername`
            -AccountPassword (ConvertTo-SecureString $_.Password -AsPlainText -Force)`
            -Givenname $_.FirstName`
            -Surname $_.LastName`
            -Emailaddress $_.Email`
            -Department $_.Department`
            -displayname $_.Name`
            -enabled $true -passwordneverexpires $true `
            -UserPrincipalName $UPN
            

            #Add user to a Group according to Department Info
            Add-ADGroupMember -Identity $_.Department -Members $_.LogonUsername;

            #Move user to an OU according to his/her Department Info
            $User = $_.LogonUsername
            $Department = $_.Department
            $UserToMove = Get-ADUser -Filter {sAMAccountName -eq $User}
            $UserToMove|Move-ADObject -TargetPath "OU=$Department,OU=TestOUs,DC=test,DC=local"

}

 

Code Example 3:We used ForEach-Object in our script. The same task could be accomplished by using ForEach instead like below. So what is the difference?

The ForEach statement loads all of the items up front into a collection before processing them one at a time. ForEach-Object expects the items to be streamed via the pipeline, thus lowering the memory requirements, but at the same time, taking a performance hit. Therefore we should use ForEach statement when we need to finish the task as fast as possible.

$UserList = Import-Csv -Path "C:\scripts\NewUsers.csv"

foreach ($Users in $UserList){
$UPN = $Users.LogonUsername + "@test.local"
$Name = $Users.Name
$SamAccountName = $Users.LogonUsername
$AccountPassword = $Users.Password
$FirstName = $Users.FirstName
$LastName = $Users.LastName
$Email = $Users.Email
$Department = $Users.Department
$DisplayName = $Users.Name

    New-ADUser -Name $Name -SamAccountName $SamAccountName -AccountPassword (ConvertTo-SecureString $AccountPassword -AsPlainText -Force) -Givenname $FirstName -Surname $LastName -Emailaddress $Email -Department $Department -displayname $DisplayName -enabled $true -passwordneverexpires $true -UserPrincipalName $UPN        

    #Add user to a Group according to Department Info
    Add-ADGroupMember -Identity $Department -Members $SamAccountName;

    #Move user to an OU according to his/her Department Info
    $UserToMove = Get-ADUser -Filter {sAMAccountName -eq $SamAccountName}
    $UserToMove|Move-ADObject -TargetPath "OU=$Department,OU=TestOUs,DC=test,DC=local"

}

 

 

Code Example 4:

We are working on the same script. This time, Firstly, we check if the user exists. If user does not exists, It creates the new user and goes on to complete the loop. If the same samaccountname exists, then it creates a log file and add an entry for each existing user and goes on to complete the loop.

$UserList = Import-Csv -Path "C:\scripts\NewUsers.csv"
$LogFile = "C:\ErrorFile.txt"


foreach ($Users in $UserList){
$UPN = $Users.LogonUsername + "@test.local"
$Name = $Users.Name
$SamAccountName = $Users.LogonUsername
$AccountPassword = $Users.Password
$FirstName = $Users.FirstName
$LastName = $Users.LastName
$Email = $Users.Email
$Department = $Users.Department
$DisplayName = $Users.Name
    
    $AdUser = Get-ADUser -Filter {sAMAccountName -eq $SamAccountName}
    If ($AdUser -eq $Null) {
            New-ADUser -Name $Name -SamAccountName $SamAccountName -AccountPassword (ConvertTo-SecureString $AccountPassword -AsPlainText -Force) -Givenname $FirstName -Surname $LastName -Emailaddress $Email -Department $Department -displayname $DisplayName -enabled $true -passwordneverexpires $true -UserPrincipalName $UPN        

            #Add user to a Group according to Department Info
            Add-ADGroupMember -Identity $Department -Members $SamAccountName;
            #Move user to an OU according to his/her Department Info
            $UserToMove = Get-ADUser -Filter {sAMAccountName -eq $SamAccountName}
            $UserToMove|Move-ADObject -TargetPath "OU=$Department,OU=TestOUs,DC=test,DC=local"
                            }
    Else{
       $TheError = "Already Exist User in Ad :$SamAccountName"
       Write-Host $TheError
       Out-File -filepath  $LogFile -InputObject $TheError -Append
       } 

}

 

 

 

  • Hits: 7341