Solved

Help with updating AD Using Powershell

Posted on 2016-08-01
11
55 Views
Last Modified: 2016-08-05
Need some help with updating multiple users in AD all at once by using a CSV file.

I currently have a CSV file of 1500 users that needs their Job title updated. These are the fields I have  

First Name     Last Name   Job Title

John                 Doe              Manager 1
Jane                 Doe              Manager 2

What would be a good way to update all the users in my csv file.  Is there a good script i can run? Do i need to change anything with my CSV file to make it easier for import?

thank you in advance for your help.
0
Comment
Question by:sirichaiphumirat
  • 6
  • 4
11 Comments
 
LVL 37

Expert Comment

by:Neil Russell
Comment Utility
There is no guarantee in ad that first name and last name make a unique I'd therefore I would use the login name as the key and have that as a field in the import.
1
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
You'll need the user's SamAccountName or DN to identify the user; example:
"SamAccountName", "Job Title"
"jdoe1","Manager 1"
"jdoe2","Manager 2"

Open in new window

Then you only need a single line of PS:
Import-Csv .\jobtitle.csv | % {Set-ADUser -Identity $_.SamAccountName -Title $_.'Job Title' -Verbose}

Open in new window

0
 

Author Comment

by:sirichaiphumirat
Comment Utility
Thank you so much.

As for the CSV file,  would i need to have a header of SamAccountName filed and Job Title filed on the top header of the CSV file?
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
Yes, because the column titles will end up as properties of the imported objects.
If you don't have a header line, you'll have to tell Import-Csv the header information in the correct order (assuming the same format as above, only without the header line):
Import-Csv .\jobtitle.csv -Header 'SamAccountName', 'Job Title' | % {Set-ADUser -Identity $_.SamAccountName -Title $_.'Job Title' -Verbose} 

Open in new window

Including a header line makes the csv way more self-explanatory, though.
0
 

Author Comment

by:sirichaiphumirat
Comment Utility
I won't be able to get the SamAccountName because its being imported from a software that doesn't hold the SamAccountname.  I'll have to use the Name filed.

Im not to great with scripts but i tried to modify yours but getting error messages. This is what I am using, could you tell me what I am doing wrong please?

I keep getting "cannot find an object with identity: 'Name' under: 'DC=XX, DC=Local"

Import-Csv .\change.csv -Header 'name', 'Title' | % {Set-ADUser -Identity $_.Name -Title $_.Title -Verbose}


my excel file looks like this
Name and title are the headers.

Name        ,     Title

John Doe ,   Manager 1
Jane Doe,     Manager 2
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
That software needs to be seriously rethought. Firstname/Lastname is not enough to uniquely identify a user in AD, unless you make hiring new people depend upon whether you already have an employee with the same name instead of qualification.
And that's how a one-liner turns into this (granted this now supports logging and some eye candy, but still):
$CsvFile = 'C:\Temp\jobtitle.csv'
$LogFile = $CsvFile -replace '\.csv\Z', '-results.csv'

$CsvUsers = Import-Csv -Path $CsvFile
$ADUsers = Get-ADUser -Filter {(GivenName -like '*') -and (Surname -like '*')} -ResultSetSize $Null | Select-Object SamAccountName, Surname, GivenName -ErrorAction Stop
$Results = ForEach ($CsvUser In $CsvUsers) {
	$Result = $CsvUser | Select-Object -Property `
		@{Name='Surname';	Expression={$_.'Last Name'}},
		@{Name='GivenName';	Expression={$_.'First Name'}},
		@{Name='JobTitle';	Expression={$_.'Job Title'}},
		SamAccountName,
		Result
	"Processing $($Result.GivenName) $($Result.Surname) " | Write-Host -ForegroundColor White -NoNewline
	If ($ADUser = $ADUsers | ? {($_.GivenName -eq $Result.GivenName) -and ($_.Surname -eq $Result.Surname)}) {
		If ($ADUser -is [array]) {
			$Result.Result = "ERROR: multiple matching accounts found: $(($ADUser | Select-Object -ExpandProperty SamAccountName) -join ', ')!"
		} Else {
			Try {
				$Result.SamAccountName = $ADUser.SamAccountName
				"($($Result.SamAccountName)) " | Write-Host -ForegroundColor White -NoNewline
				Set-ADUser -Identity $Result.SamAccountName -Title $Result.JobTitle -ErrorAction Stop
				$Result.Result = 'OK'
			} Catch {
				$Result.Result = 'ERROR: $($_.Exception.Message)'
			}
		}
	} Else {
		$Result.Result = 'ERROR: no matching account found!'
	}
	$ResultColor = If ($Result.Result.StartsWith('ERROR')) {[ConsoleColor]'Red'} Else {[ConsoleColor]'Green'}
	"... $($Result.Result)" | Write-Host -ForegroundColor $ResultColor
	$Result
}
If (-not [string]::IsNullOrEmpty($LogFile)) {
	$Results | Export-Csv -Path $LogFile -NoTypeInformation
}

Open in new window

0
 

Author Comment

by:sirichaiphumirat
Comment Utility
oh wow thank you, will check this out. Yeah really hate the software we use
0
 

Author Comment

by:sirichaiphumirat
Comment Utility
what should the header be to be able to run this script correctly?

I was able to run it, but it didn't read anything, i just got processing ... error: no matching account found:

thank you again for helping out.
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
As described in your question:
"First Name","Last Name","Job Title"
0
 

Author Comment

by:sirichaiphumirat
Comment Utility
thank you again for your help
0
 

Author Closing Comment

by:sirichaiphumirat
Comment Utility
thank you again!!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now