Mass edit individual attribute in Active Directory from source data

511 views Asked by At

At work we just got a whole new range of phone numbers. This means that every phonenumber is changed. This needs to be reflected in our Active Directory for easy access and other dependancies. There is a simple excel file which includes the following attributes (phonenumber) (extension) (first lastname). The extension can be deleted since it is included in the main number, so that is not important. Unfortunately the source file does not include an UPN.

I have looked at options to do this, a whole bunch of them were suggested here. However, all those that could possible do what we need are based on installed software. This is however not an option on our server.

Therefore my question is, is there a way to mass edit everything in a command promt or powershell? I would like to do something like this, but then something for unique attributes for specified users;

get-aduser -filter { city -like 'Glitter*' } | set-aduser -city 'Las Vegas' -PostalCode 89123 –PassThru 

(reference)

1

There are 1 answers

2
Matt On

Assuming your source file is set up like it is in your question this should be a good start for what you need. Convert your source file to a csv to save you some headache. This assumes that the first line is first lastname,phonenumber,extension. Order does not matter.

# Collect the source data as a powershell object we can process in a loop
$list = Import-CSV "C:\temp\sourcedata.csv"

# Process each user individually
ForEach($user in $list){
    # Get the user matching the display name
    $results = Get-ADUser -filter "DisplayName -eq '$($user."First LastName")'"

    # If a user was returned set the phone number
    If($results){
        $results | Set-ADUser -OfficePhone $user.phonenumber -WhatIf
    } Else {
        Write-Warning "$($user.'First LastName') not found"
    }
}

We use Get-ADUser to find the "one" user and make changes based the record in your source file. You can leave -Whatif in there for testing. PowerShell will tell you it is editing so you can get a feel for what is happening. We are assuming -OfficePhone is the attribute you are updating.