Change user Active Directory attributes stored in a CSV file
Change user Active Directory attributes stored in a CSV file

This script change phone number and mobile phone number attributes for users based on a CSV file with the following template :

Lastname;Firstname;Username;MobilePhone;Phone
Smith;John;jsmith;+33xxxxxxxxx;+33xxxxxxxxx
Doe;John;jdoe;+33xxxxxxxxx;+33xxxxxxxxx

If you want to use the second script below (with the Quest cmdlets), just remove the header line of the csv file. This script uses a simple get-content to load csv file information

Script (with Microsoft Active Directory module loaded : import-module activedirectory) :

$csv = import-csv c:\folder\file.csv -Delimiter ";"
foreach ($line in $csv) {
	$username = $line.Username
	$tel = $line.Phone
	$mobile = $line.MobilePhone
	
	Set-ADObject -identity (Get-ADUser $username).DistinguishedName -Add @{telephoneNumber=$tel;mobile=$mobile} 
}

Different way using the Quest Active Directory module :

$csv = Get-Content c:\folder\file.csv
foreach ($string in $csv) {
	$username = $string.split(";")[2]
	$tel = $string.split(";")[4]
	$mobile = $string.split(";")[3]
	$username + "`t : " + `
		(Get-QADObject -Type User -SizeLimit 0 -Identity $username -Properties telephonenumber).telephonenumber + "`t/`t" + `
		(Get-QADObject -Type User -SizeLimit 0 -Identity $username -Properties mobile).mobile | `
		Out-File -FilePath ("c:\folder\change.log") -encoding ASCII -append -Width 1000

	if ($mobile -eq "NULL") {
		Set-QADUser -Identity $username -PhoneNumber $tel -MobilePhone $null
	}
	else {
		Set-QADUser -Identity $username -PhoneNumber $tel -MobilePhone $mobile
	}
	"`t > NEW ENTRY for " + $username + "`t : " + `
	(Get-QADObject -Type User -SizeLimit 0 -Identity $username -Properties telephonenumber).telephonenumber + "`t/`t" + `
	(Get-QADObject -Type User -SizeLimit 0 -Identity $username -Properties mobile).mobile | `
	Out-File -FilePath ("c:\folder\change.log") -encoding ASCII -append -Width 1000
}

<>

My Powershell script categories

Change user Active Directory attributes stored in a CSV file

Leave a Reply

Your email address will not be published.