Hey everyone!

I’m currently doing a test where I have to add 1,000 users into Active Directory, specifically into our “Users” and “Marketing” Organizational Units. I have a .csv file containing the (fake) first and last names, as well as the OU their supposed to go into, as well as the passwords that were generated for them. For the past hour and a half, I’ve tried importing the .csv file into Active Directory using a Power Shell script I made, but the script looks like it runs, but it doesn’t actually do anything. I’ll be the first to admit that I’m not the best at scripting, so if anyone can take a look at my script and/or .csv file, that would be awesome!

Also, is there a way in the Power Shell script that I can generate a user’s logon name as well? I tried to do it in the script, but I’m not sure if it’s correct.

.csv is attatched and named “Testing.csv”

Powershell Code:

Import-Module ActiveDirectory

$Domain = @"dmc.com"

$UserOU = "OU=Marketing, OU=Users"

$NewUsersList = Import-Csv "C:\Testing.csv"

ForEach ($User in $NewUsersList)
{
    $FirstName = $User.FirstName
    
    $LastName = $User.LastName

    $FullName = $User.FirstName + $User.LastName

    $LogonName = $FullName

    $Password = $User.Password

    $UserLogonName = $FullName
}

Testing.txt (6.92 KB)

5 Spice ups

Looks like you are missing any code that actually creates the user in Active Directory you likely need to add something like New-ADUser https://docs.microsoft.com/en-us/powershell/module/addsadministration/new-aduser?view=win10-ps into your ForEach loop to actually add the user in AD. I beleive you will also need to convert the temp password to a securestring before you can pass it to new-ADUser.

check this out

https://community.spiceworks.com/scripts/show/3682-bulk-create-active-directory-users-powershell-with-logs-less-rows-in-csv

Also if you have a standard username format, say for example first initialLastName ex. my name is Alex Scheidel my username would be ascheidel, then you can use string manipulation to get what you wast something like

$UserName = $FirstName.Substring(0,1) + $LastName

As you might have suplicates of that, lets say your standard is to add a number at the end of the username for additional people with the same username you could test using something like

$Username = $FirstName.SubString(0,1) + $LastName
$Existing = get-aduser -filter * | ? { $_.samaccountname -match $Username }
If($Existing){
    $i=0
    ForEach($Exists in $Existing{
        If(($Exists.SamAccountName.Length -gt $Username.Length) -AND (($Exists.SamAccountName.Substring($UserName.Length,($Exists.SamAccountName.Length - $UserName.Length))) -match '^[0-9]+$')){
            $t = ($Exists.SamAccountName.Substring($UserName.Length,($Exists.SamAccountName.Length - $UserName.Length)) -as [int]
            If($t -gt $i){
               $i = $t + 1
            }
            Else{
               $i = ($Exists.SamAccountName.Substring($UserName.Length,($Exists.SamAccountName.Length - $UserName.Length)) -as [int] + 1
            }
        Else{
            $i = 1
        }
    }
    $UserName = $UserName + $i.ToString()
}