Powershell to update users in ADUC

I need to run an update for all users in the user aduc with PowerShell to update the following fields.
The PowerShell script needs to read the information from a csv file that will then go to ad and update the information

I need to key off of the user samaccountname and update the following :
Department
Title
Manager
Telephone
Office
Address
city
state
zipcode
gd6627Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Try this; the csv should have the column names you listed (plus SamAccountName, obviously).
The "Manager" column should contain the respective manager's SamAccountName.
$managers = @{}
Import-Csv -Path C:\Temp\users.csv | ForEach-Object {
	"Processing $($_.SamAccountName) ..." | Write-Host
	If (-not $managers.ContainsKey($_.Manager)) {
		"  Caching manager '$($_.Manager)' ..." | Write-Host
		$managers[$_.Manager] = (Get-ADUser -Identity $_.Manager).DistinguishedName
	}
	$splat = @{
		Department = $_.Department
		Title = $_.Title
		Manager = $managers[$_.Manager]
		OfficePhone = $_.Telephone
		Office = $_.Office
		StreetAddress = $_.Address
		City = $_.City
		State = $_.State
		PostalCode = $_.ZipCode
	}
	Set-ADUser -Identity $_.SamAccountName @splat
}

Open in new window

0
gd6627Author Commented:
Hi Thanks can you explain so I understand what your script is actually doing , from what I tried to interpret you r importing the csv but the if statement for manager I need to understand for my own knowledge of how it works

Thank you
0
oBdACommented:
Thank you for that question. It's actually not necessary. Somehow, I was under the impression that the Manager required the manager's DistinguishedName, but it actually accepts the SamAccountName as well.
Anyway, $managers was just a hash table to avoid repeated AD queries for the same manager. A new entry was added when a manager was found for which the DN wasn't retrieved yet.
So here's the simplified version (still requiring the Manager's column to contain the manager's SamAccountName (or DistinguishedName, but SamAccountName is obviously easier to read ...).
Import-Csv -Path C:\Temp\users.csv | ForEach-Object {
	"Processing $($_.SamAccountName) ..." | Write-Host
	$splat = @{
		Department = $_.Department
		Title = $_.Title
		Manager = $_.Manager
		OfficePhone = $_.Telephone
		Office = $_.Office
		StreetAddress = $_.Address
		City = $_.City
		State = $_.State
		PostalCode = $_.ZipCode
	}
	Set-ADUser -Identity $_.SamAccountName @splat
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gd6627Author Commented:
thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.