Hello folks,

I suck at Powershell. Just putting that out there. I need to update the following AD attributes in Powershell for a list of users from a csv file:

Manager

Job Title

Office

Employee ID

Department

I need to do this using their email address as the unique identity for each object. Can anyone provide a sample script and how my CSV should be formatted to accomplish this? If you need details, please let me know. Thanks!

2 Spice ups

Welcome.

What have you tried? Where are you stuck

In general you can use ‘set-aduser’ to update attributes, there are a bunch of build in ones.

Set-ADUser $userName -manager $manager

I tried this so far which I think you answered on another thread lol:

Import-module ActiveDirectory
$users = Import-Csv “C:\Temp\users.csv”
foreach ($user in $users) {
$props = @{
identity = $user.samaccountname
title = if($user.JobTitle ){$user.JobTitle }else{$null}
Office = if($user.Office ){$user.Office }else{$null}
department = if($user.Department ){$user.Department }else{$null}
manager = if($user.manager ){$user.manager }else{$null}
EmployeeID = if($user.employeeid ){$user.employeeid }else{$null}
}
set-aduser @props
}

But I get an error: Set-ADUser : Cannot validate argument on parameter ‘Identity’. The argument is null. Provide a valid value for the
argument, and then try running the command again.
At C:\Temp\attribute script.ps1:12 char:16

ya that points to the CSV.

What does it currently look like? You CSV should NOT have empty values if possible.

it should look like so:

samaccountname,jobtitle,office,department,manager,employeeid
user01,Title01,office1,department1,manager1,12345
user02,Title02,office2,department2,manager2,23456

Also… if you post code, please use the ‘Insert Code’ button. Please and thank you!

192033ab-bb8f-4032-88a5-8e2313af0344-codebutton_small.png

  • Neally Bot

Thanks Neally for the super fast reply! So now it’s asking this:

cmdlet Set-ADUser at command pipeline position 1
Supply values for the following parameters:
Identity:

Here is my csv headers:

Samaccountname,jobtitle,Office,Department,manager,employeeid

Samaccountname,jobtitle,Office,Department,manager,employeeid

what if you do it ‘manually’ for one user?

set-aduser -Identity $username -title $jobtitle  -Office $office -Department $department -Manager $manager -EmployeeID $EID

make sure that each item has a value in the CSV

$csv = import-csv "./yourCSVfile.csv"

foreach($row in $csv){
    $row.samaccountname
    $row.JobTitle
    $row.Office
    $row.Department
    $row.manager
    $row.employeeid
}

It gives me an error saying Cannot fine object with identity: johndoe@contoso.com under:

Can I change the default OU to the OU I know the user is in? Would that maybe help?

It gives me an error saying it can’t find the object with identity but it doesn’t appear to be searching the right ou. Is there a way to change the default ou it searches?

what happens when you use ‘get-aduser’ instead?

Get-ADuser $row.samaccountname

it by default looks in all OUs… so I’d double-check the samaccountname.


```

```

That did it. It was the samaccountname I had wrong. I was adding the domain to the end of it. I’m good now Neally! Thank you so much!!!

Get-ADUser does not recognize UPNs outside of the -filter parameter.

1 Spice up