Jakodns
Jakodns

Reputation: 344

Powershell Update Active Directory Users Based on Delimited File

If I have two rows in a CSV file like below (but 200 entries or so).

$Username        $Phone
Example.A        911
Example.B        123

How would I get PowerShell to take each row and enter it into a command?

I know how to do it if the phonenumber (Using a for each loop) is the same for everyone, but if everyone has a different phonenumber, how would I go about it then? Can I use a for each loop fetching two rows instead of 1 row from the csv file?

Foreach $Username, $Phonenumber in $CSV
Set-ADuser -identity $Username -PhoneNumber $Phonenumber

Above is how I would like it to be, but that doesn't work.

Upvotes: 0

Views: 35

Answers (1)

morgb
morgb

Reputation: 2312

To do what you're attempting, you just need a couple of slight modifications as follows:

$CSV = Import-Csv -Delimiter "," -Path "\\your\file\path" 
foreach ($user in $CSV) {
    Set-ADuser -identity $user.Username -PhoneNumber $user.Phonenumber
}

Upvotes: 1

Related Questions