Powershell – Updating Active Directory Objects Using from a CSV


Below are possible ways to update active directory objects using data contained in a CSV.

Without a CSV:

Get-ADUser Soma.Bright -Properties SamAccountName | Set-ADUser -Replace @{SamAccountName="Bright.Soma"}

Using a CSV with “identity”:

$file=import-csv "AccountInfo.csv"
foreach($dataRecord in $file) {
$SamAccountName=$dataRecord.SamAccountName

$EmployeeType=$dataRecord.employeeType
$Department=$dataRecord.department

Get-ADUser -Identity $SamAccountName -Properties EmployeeType | Set-ADUser -Replace @{EmployeeType=$EmployeeType}

Get-ADUser -Identity $sAMAccountName | Set-ADUser -Department $Department
}

Using a CSV with a “filter”:

Import-module ActiveDirectory 
$userList= Import-Csv '.\List of Users.csv'
foreach ($userin$userList){ Get-ADUser -Filter "SamAccountName -eq '$($user.sAMAccountName)'"-SearchBase "DC=subdomain,DC=company,DC=com"-Properties Company |% { Set-ADUser $_-Replace@{Company = 'Deliveron'} } }If you then wanted to query AD for those users to make sure they updated correctly, you could use the following query using Get-ADUser:foreach ($userin$userList){

Get-ADUser -Filter "SamAccountName -eq '$($user.sAMAccountName)'"-SearchBase "DC=subdomain,DC=company,DC=com"-Properties Company | Select SamAccountName, Name, Company

}

Updating Multiple values:

 Import-Module ActiveDirectory  

$users = Import-Csv -Path c:\update.csv

foreach ($user in $users) {
Get-ADUser -Filter "employeeID -eq '$($user.employeeID)'" -Properties * -SearchBase "ou=Test,ou=OurUsers,ou=Logins,dc=domain,dc=com" |
Set-ADUser -employeeNumber $($user.employeeNumber) -department $($user.department) -title $($user.title) -office $($user.office) -streetAddress $($user.streetAddress) -City $($user.City) -state $($user.state) -postalCode $($user.postalCode) -OfficePhone $($user.OfficePhone) -mobile $($user.mobile) -Fax $($user.Fax) -replace @{"extensionAttribute1"=$user.extensionAttribute1; "extensionAttribute2"=$user.extensionAttribute2; "extensionAttribute3"=$user.extensionAttribute3}
}

For more info please see the below links:

Import-csv to update Active directory users

Update AD from CSV

Powershell – CSV – AD Attribute Update

AD / Import CSV Update Attributes Script

Import Users from a CSV File into Active Directory using PowerShell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s