Powershell to update users in ADUC

gd6627
gd6627 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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

Author

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
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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

Author

Commented:
thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial