I’m trying to use Powershell to bulk update user details within AD. My CSV file has the following fields “sAMAccountName ; title ; department ; description ; emailaddress ; mobilephone ; company ; officephone”

When I ran the script, instead of updating/adding the information from the CSV, it cleared all information for every user.

I don’t know anyone who knows Powershell, so any assistance is greatly appreciated. I know something is missing, but I don’t know what… Thank you, code below.

Import-Module ActiveDirectory
Import-CSV "C:\aduserupdate.csv"
$SAM = $_.sAMAccountName
$title = $_.title
$department = $_.department
$description = $_.description
$mail = $_.EmailAddress
$mobile = $_.MobilePhone
$company = $_.Company
$ipPhone = $_.OfficePhone
Get-ADUser -filter * -SearchBase "ou=Users,dc=internal,dc=fake,dc=com" -Properties Company,Department,Description,EmailAddress,MobilePhone,Office,OfficePhone,Title | ForEach-Object {
Set-ADUser -Identity $_.sAMAccountName -Company $company -Department $department -Description $_.description -EmailAddress $mail -MobilePhone $mobile -Office $company -OfficePhone $ipPhone -Title $title}
8 Spice ups

Also, i tried to use the $_ pipeline variable for description instead of using a defined variable with hopes to see a change in result. This didnt work either.

Set-ADUser -Identity $.sAMAccountName -Company $company -Department $department -Description **$.description** -EmailAddress $mail -MobilePhone $mobile -Office $company -OfficePhone $ipPhone -Title $title}

Can you give an example of your CSV?

It doesn’t look like you are looping through your CSV objects.

Also, you are performing a get-aduser that returns all AD objects, then you are writing invalid (i.e. blank) entries to each one. At the very best of situations, you probably would have written the same information to every user.

Just clarifying this for other readers of this topic…(you pretty much knew that you goofed up the user objects!).

1 Spice up

You’d need something like this:

Import-CSV c:\aduserupdate.csv | Foreach-Object{
$SAM = $_.sAMAccountName
$title = $_.title
$department = $_.department
$description = $_.description
$mail = $_.EmailAddress
$mobile = $_.MobilePhone
$company = $_.Company
$ipPhone = $_.OfficePhone
#---you don't need this--- Get-ADUser -filter * -SearchBase "ou=Users,dc=internal,dc=fake,dc=com" -Properties Company,Department,Description,EmailAddress,MobilePhone,Office,OfficePhone,Title | ForEach-Object {
Set-ADUser -Identity $_.sAMAccountName -Company $company -Department $department -Description $_.description -EmailAddress $mail -MobilePhone $mobile -Office $company -OfficePhone $ipPhone -Title $title
#---you don't need this}
   
}

here’s the first 3 lines (header and two lines) with confidential information edited.

sAMAccountName,title,department,description,EmailAddress,MobilePhone,Company,OfficePhone

aagundiz,Field Engineer,Concrete,Field Engineer,email@domain.com,Concrete,111-222-3333

aalexander,Senior Superintendent,Superintendent,Senior Superintendent,email2@domain.com,444-555-6767,Builders,888-999-0000

Import-Module ActiveDirectory
Import-CSV "C:\aduserupdate.csv"
$SAM = $_.sAMAccountName
$title = $_.title
$department = $_.department
$description = $_.description
$mail = $_.EmailAddress
$mobile = $_.MobilePhone
$company = $_.Company
$ipPhone = $_.OfficePhone
Set-ADUser -Identity $_.sAMAccountName -Company $company -Department $department -Description $_.description -EmailAddress $mail -MobilePhone $mobile -Office $company -OfficePhone $ipPhone -Title $title

Doing this outputs the following:

Set-ADUser : Cannot validate argument on parameter ‘Identity’. The argument is null. Supply a non-null argument and try the command again.

At line:11 char:22

  • Set-ADUser -Identity $_.sAMAccountName -Company $company -Department $department …

  • 
    
  • CategoryInfo : InvalidData: (:slight_smile: [Set-ADUser], ParameterBindingValidationException

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

I just tried this using the CSV format you gave me and it is working - although it did bug out on me with the mobilephone field since it was null.

Are you sure your samAccountName is correct? You can try it manually by performing

Set-ADUser -identity aagundiz -company “Concrete”

2 Spice ups

The manual action worked (Set-ADUser -identity aagundiz -company “Concrete”). I verified the sAMAccountName’s and they’re correct.

Out of curiosity, what version of Powershell are you running?

You can get this information by running

$PSVersionTable.PSVersion

Are you running it as an elevated user?

Additionally, put this line above set-aduser:

get-variable | out-string

Do you see $SAM being populated when you run it?

Valentin, please copy and part the coffee that Rob mentioned. The code you just out into coffee blocks is the same coffee that you posted in the original post. You still don’t have the import-csv piping to for-eachobject as Rob suggested doing.

You are also setting a bunch of variables at the top and then in the set-aduser you are using some of the variables but then using what some of them equal, for instance $.samAccountName you defines as $SAM but never used $SAM in the set had user you used $.samAccountName instead. I would suggest not defining any variables at the top and just use the $_.csvColumnNameHere

Hope that makes sense. I typed all of this on my phone. Sorry I can’t quote messages on the mobile website.

1 Spice up

I finally got it working with the following code:

Import-Module ActiveDirectory
$users = Import-CSV "C:\aduserupdate.csv"
$SAM = $_.sAMAccountName
$title = $_.title
$department = $_.department
$description = $_.description
$mail = $_.EmailAddress
$mobile = $_.MobilePhone
$company = $_.Company
$ipPhone = $_.OfficePhone
foreach ($user in $users) {
Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * -SearchBase "ou=users,dc=internal,dc=fake,dc=com"
Set-ADUser -Identity $user.sAMAccountName -Company $($user.Company) -Department $($user.department) -Description $($user.description) -EmailAddress $($user.emailaddress) -MobilePhone $($user.MobilePhone) -Office $($user.Company) -OfficePhone $($user.OfficePhone) -Title $($user.title)
}

Also to note, If any of the fields were null, none of the user info would update. I had to replace all null fields with “null” or “000-000-0000” for phone numbers. Once I did that, all data imported successfully. Thanks for your help Rob!

Import-Module ActiveDirectory
$users = Import-CSV "C:\aduserupdate.csv"
foreach ($user in $users) {
Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * -SearchBase "ou=builders,dc=internal,dc=fake,dc=com"
Set-ADUser -Identity $user.sAMAccountName -Company $($user.Company) -Department $($user.department) -Description $($user.description) -EmailAddress $($user.emailaddress) -MobilePhone $($user.MobilePhone) -Office $($user.Company) -OfficePhone $($user.OfficePhone) -Title $($user.title)
}

Here’s the cleaned up code without all the variables. Thanks again for everyone’s help

Why are you running get-aduser? It’s not doing anything other than reporting back user information.

1 Spice up

i couldn’t get the set-aduser command to accept without using get-aduser… i tried without and i threw back the same “identity” error… not sure why… but i’m not arguing it. I’ve been banging my head over this for 3 days now. I’m just happy it worked :).

Very odd. For what it’s worth, I did not have to run get-aduser here, and you never answered my question about what PS version you were running…!

I’m not sure why it worked for you and not for me… then again, I dont know much about powershell.

Sorry! I didn’t see those messages above my solution, must have needed to refresh.

Version: Major = 3 Minor = 0 Build = -1 Revision = -1

Because in this post:

You did not have the Import-CSV piping to For-Each like I mentioned in this Post:

It should work without Get-ADUser as Rob mentioned. It is fairly inefficient to call Get-ADUser when you don’t have to. You should only call Get-ADUser to find usernames that you don’t know. You have a CSV file that has the username’s you want, so All you need is Set-ADUser

The correct code should look like this.

Import-Module ActiveDirectory
Import-CSV c:\aduserupdate.csv | Foreach-Object{
	Set-ADUser -Identity $_.sAMAccountName -Company $_.Company -Department $_.department -Description $_.description -EmailAddress $_.EmailAddress -MobilePhone $_.MobilePhone -Office $company -OfficePhone $_.OfficePhone -Title $_.title
}

Which believe it or not, without the Import-Module Line, this just turned your script into a one-liner.

And then just for ease of reading the one-liner, you could separate each row with the backtick ( ` ) symbol like so:

Import-Module ActiveDirectory
Import-CSV c:\aduserupdate.csv | Foreach-Object{
	Set-ADUser -Identity $_.sAMAccountName `
	-Company $_.Company `
	-Department $_.department `
	-Description $_.description `
	-EmailAddress $_.EmailAddress `
	-MobilePhone $_.MobilePhone `
	-Office $company `
	-OfficePhone $_.OfficePhone `
	-Title $_.title
}
3 Spice ups

Cool thanks Chamele0n!