You have a CSV file that has data that needs to be added to Active Directory you say?

You aren't going to believe this, but today's your lucky day. Funnel that CSV file into this script, and you're on your way to happiness.

But seriously, this is something I assembled and cleaned up from the remnants of another script. Simple, easy to understand, and quick. I processed records for just over 300 users in 42 seconds. That'll do.

# Update AD User Data
# Source data in CSV format
# 2018 - Joel Spangler
# Bell and Evans

# Get script Start Time
$startTime = (Get-Date)

# set path to input file
$csvfile = "C:\scripts\ADImport\adexport.csv"

# Specify Domain
$ADServer = 'farmerspride.com'

# Import the AD Module
Import-Module ActiveDirectory


# Create Function to import data from CSV and perform corresponding AD Updates

Function Update-ADUsers {

    Import-Csv -path $csvfile | `
    ForEach-Object {
        # Each of the following corresponds to the Fields defined in the CSV file
        $DisplayName = $_.Name
        $Description = $_.Description
        $Department = $_.Department
        $Title = $_.'Job Title'
        $Office = $_.Office
        $Mobile = $_.'Mobile Phone Number'
        $Phone = $_.Telephone
        $sam = $_.Username
        $Address = $_.StreetAddress
        $State = $_.State
        $Company = $_.Company
        $Country = $_.Country
        $City = $_.City

        # Manager Field takes some extra work.
        $Manager = $_.Manager
        if ($Manager -ne $null){$Manager = Get-ADUser -Filter {Name -like $Manager} | select -Expand DistinguishedName}

        # Verify AD Account
        Try { $SAMinAD = Get-ADUser -server $ADServer -LDAPFilter "(sAMAccountName=$sam)"} 
        Catch { }

        # If statement - Valid If AD user exists
            If($SAMinAD -ne $null -and $sam -ne '') {

            # evaluate each field - if data us present - update corresponding field - match each field to ADSI attribute name
            If ($DisplayName -ne '') { Set-ADUser -server $ADServer -Identity $sam -Replace @{displayName=$DisplayName} }
            If ($DisplayName -ne '') { Set-ADUser -server $ADServer -Identity $sam -Replace @{cn=$DisplayName} }
            If ($Description -ne '') { Set-ADUser -server $ADServer -Identity $sam -Replace @{description=$Description} }
            If ($Department -ne '') { Set-ADUser -server $ADServer -Identity $sam -Replace @{department=$Department} }
            If ($Title -ne '') { Set-ADUser -server $ADServer -Identity $sam -Replace @{title=$Title} }
            If ($Manager -ne '') { Set-ADUser -server $ADServer -Identity $sam -Replace @{manager=$Manager} }
            If ($Office -ne '') { Set-ADUser -server $ADServer -Identity $sam -Replace @{physicalDeliveryOfficeName=$Office} }
            If ($Mobile -ne '') { Set-ADUser -server $ADServer -Identity $sam -Replace @{mobile=$Mobile} }
            If ($Phone -ne '') { Set-ADUser -server $ADServer -Identity $sam -Replace @{telephoneNumber=$Phone} }
            If ($Address -ne '') { Set-ADUser -server $ADServer -Identity $sam -Replace @{streetAddress=$Address} }
            If ($State -ne '') { Set-ADUser -server $ADServer -Identity $sam -Replace @{st=$State} }
            If ($Company -ne '') { Set-ADUser -server $ADServer -Identity $sam -Replace @{company=$Company} }
            If ($Country -ne '') { Set-ADUser -server $ADServer -Identity $sam -Replace @{co=$Country} }
            If ($City -ne '') { Set-ADUser -server $ADServer -Identity $sam -Replace @{l=$City} }


            }

            Else { 
            
            #Error - AD User does not exist
            " User $sam not found in AD "
            
            }
         

        }

}
#End Function


#Run Function
Update-ADUsers

# Set End Time
$endTime = (Get-Date)

# Calcualate total time to run. Unnecessary, but fun.
"Elapsed Time: $(($endTime-$startTime).totalseconds) seconds"
"Elapsed Time: $(($endTime-$startTime).totalminutes) minutes"