Import-module ActiveDirectory

$data = import-csv “C:\Users\me\Documents\PowerSHellScripts\test1.csv”

foreach ($user in $data){

Get-ADUser -Filter “SamAccountName -eq ‘$($user.employeename)’” | Set-ADUser -Replace @{description=“$($user.description)”;physicalDeliveryOfficeName=“$($user.Office)”;Telephonenumber=“$($user.Telephonenumber)”;mail=“$($user.emailaddress)”;streetAddress=“$($user.street)”;l=“$($user.city)”;st=“$($user.state)”;postalCode=“$($user.postalCode)”;co=“$($user.country)”;mobile=“$($user.mobile)”;facsimileTelephoneNumber=“$($user.fax)”;Title=“$($user.title)”;Department=“$($user.department)”;Company=“$($user.company)”;manager=“$($user.manager)”;wWWHomePage=“$($user.webpage)”}

}

Trying to bulk update AD user properties. Script works for single row in csv but does not cycle though csv if more than one row.

–CSV–

Employeename, description, office, telephonenumber, emailaddress, street, city, state, postalcode, country, mobile, fax, title, department, company, manager, webpage

4 Spice ups

http://community.spiceworks.com/topic/409427-please-read-before-posting

Use the code button when posting code examples. Makes them easier to read.

2 Spice ups
Import-module ActiveDirectory
$data = import-csv "C:\Users\me\Documents\PowerSHellScripts\test1.csv"

foreach ($user in $data){
Get-ADUser -Filter "SamAccountName -eq '$($user.employeename)'" | Set-ADUser -Replace @{description="$($user.description)"`
;physicalDeliveryOfficeName="$($user.Office)";Telephonenumber="$($user.Telephonenumber)";mail="$($user.emailaddress)"`
;streetAddress="$($user.street)";l="$($user.city)";st="$($user.state)";postalCode="$($user.postalCode)";co="$($user.country)"`
;mobile="$($user.mobile)";facsimileTelephoneNumber="$($user.fax)";Title="$($user.title)";Department="$($user.department)"`
;Company="$($user.company)";manager="$($user.manager)";wWWHomePage="$($user.webpage)"}
}

:slight_smile:

Import-module ActiveDirectory
$data = import-csv "C:\Users\smitchell\Documents\PowerSHellScripts\test1.csv"
foreach ($user in $data){
	Get-ADUser -Filter "SamAccountName -eq '$($user.employeename)'" | 
	Set-ADUser -Replace @{
		description=$user.description
		physicalDeliveryOfficeName=$user.Office
		Telephonenumber=$user.Telephonenumber
		mail=$user.emailaddress
		streetAddress=$user.street
		l=$user.city
		st=$user.state
		postalCode=$user.postalCode
		co=$user.country
		mobile=$user.mobile
		facsimileTelephoneNumber=$user.fax
		Title=$user.title
		Department=$user.department
		Company=$user.company
		manager=$user.manager
		wWWHomePage=$user.webpage
	}
}

Here is a way to lay it out that makes it easier to read and troubleshoot.

As to your problem, what is the output of this; does it show all the users’ SAMs?

Import-module ActiveDirectory
$data = import-csv "C:\Users\smitchell\Documents\PowerSHellScripts\test1.csv"
foreach ($user in $data){
	Write-Output $user.employeename
}
2 Spice ups

Yes the out put lists all SAM’s in the csv list

1 Spice up

Does this list all the AD user objects?

Import-module ActiveDirectory
$data = import-csv "C:\Users\smitchell\Documents\PowerSHellScripts\test1.csv"
foreach ($user in $data){
	Get-ADUser -Filter "SamAccountName -eq '$($user.employeename)'"
}
1 Spice up

Concerning the original script, were there any errors or warnings?

Is every field in the csv, for every user, populated; any blanks?

1 Spice up

OK, so a space behind the second SAM account name was the culprit. I was only testing with 2 rows to be safe.

All set

Getting this error when i try to import.

Set-ADUser : replace

At line:16 char:12

  • Set-ADUser <<<< -Replace @{

  • CategoryInfo : InvalidOperation: (CN=Aigul Alliso…DC=dawood,DC=cc:ADUser) [Set-ADUser], ADInvalidOperationException

  • FullyQualifiedErrorId : replace,Microsoft.ActiveDirectory.Management.Commands.SetADUser

Is every field in the csv, for every user, populated; no blanks?

No, the telephone field has some users with black and the mobile field has blanks as well. Just because they do not have numbers. I will populate with something and try again.

foreach ($user in $data){
	$replace = @{}
	
	If($user.description) { $replace['description']=$user.description }
	If($user.Office) { $replace['physicalDeliveryOfficeName']=$user.Office }
	#And so on....

	Get-ADUser -Filter "SamAccountName -eq '$($user.employeename)'" | 
	Set-ADUser -Replace $replace
}

Need to do something like this. You can only pass -Replace things that have values.

Pretty much updating all the fields listed in CSV, would it be easier to -clear said fields and then do -add

1 Spice up

Then you could only pass -Add fields that had values and you’d be in the same boat.

OK got it sorted out. Thanks for all your help!!!

Import-module ActiveDirectory
$data = import-csv "C:\Users\smitchell\Documents\PowerSHellScripts\test1.csv"

$lookup = @{
    "description" = "description"
    "Office" = "physicalDeliveryOfficeName"
    "Telephonenumber" = "Telephonenumber"
    "emailaddress" = "mail"
    "street" = "streetAddress"
    "city" = "l"
    "state" = "st"
    "postalCode" = "postalCode"
    "country" = "co"
    "mobile" = "mobile"
    "fax" = "facsimileTelephoneNumber"
    "title" = "Title"
    "department" = "Department"
    "company" = "Company"
    "manager" = "manager"
    "webpage" = "wWWHomePage"
}

$fields = $data | 
Get-Member -MemberType NoteProperty | 
Select-Object -ExpandProperty Name | 
Where-Object { $_ -ne "employeename" }

ForEach ($user in $data) {
    $replace = @{}
    ForEach( $field in $fields) {
        If($user.$field) {
            $replace[$lookup[$field]] = $user.$field
        }
    }
    Get-ADUser -Filter "SamAccountName -eq '$($user.employeename)'" |
    Set-ADUser -Replace $replace
}

You could try that, but it is only lightly tested.

2 Spice ups

Thank that worked great!