\nFullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser<\/p>\n<\/li>\n<\/ul>","upvoteCount":0,"datePublished":"2014-07-10T11:14:32.000Z","url":"https://community.spiceworks.com/t/powershell-to-import-into-ad/320026/6","author":{"@type":"Person","name":"valentinarroyo8372","url":"https://community.spiceworks.com/u/valentinarroyo8372"}},{"@type":"Answer","text":"
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.<\/p>\n
Are you sure your samAccountName is correct? You can try it manually by performing<\/p>\n
Set-ADUser -identity aagundiz -company “Concrete”<\/p>","upvoteCount":2,"datePublished":"2014-07-10T11:30:27.000Z","url":"https://community.spiceworks.com/t/powershell-to-import-into-ad/320026/7","author":{"@type":"Person","name":"Rob-Dunn","url":"https://community.spiceworks.com/u/Rob-Dunn"}},{"@type":"Answer","text":"
The manual action worked (Set-ADUser -identity aagundiz -company “Concrete”). I verified the sAMAccountName’s and they’re correct.<\/p>","upvoteCount":0,"datePublished":"2014-07-10T12:57:38.000Z","url":"https://community.spiceworks.com/t/powershell-to-import-into-ad/320026/8","author":{"@type":"Person","name":"valentinarroyo8372","url":"https://community.spiceworks.com/u/valentinarroyo8372"}},{"@type":"Answer","text":"
Out of curiosity, what version of Powershell are you running?<\/p>\n
You can get this information by running<\/p>\n
$PSVersionTable.PSVersion<\/p>\n
Are you running it as an elevated user?<\/p>","upvoteCount":0,"datePublished":"2014-07-10T13:23:03.000Z","url":"https://community.spiceworks.com/t/powershell-to-import-into-ad/320026/9","author":{"@type":"Person","name":"Rob-Dunn","url":"https://community.spiceworks.com/u/Rob-Dunn"}},{"@type":"Answer","text":"
Additionally, put this line above set-aduser:<\/p>\n
get-variable | out-string\n<\/code><\/pre>\nDo you see $SAM being populated when you run it?<\/p>","upvoteCount":0,"datePublished":"2014-07-10T13:24:43.000Z","url":"https://community.spiceworks.com/t/powershell-to-import-into-ad/320026/10","author":{"@type":"Person","name":"Rob-Dunn","url":"https://community.spiceworks.com/u/Rob-Dunn"}},{"@type":"Answer","text":"
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.<\/p>\n
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 $<\/em>.samAccountName instead. I would suggest not defining any variables at the top and just use the $_.csvColumnNameHere<\/p>\nHope that makes sense. I typed all of this on my phone. Sorry I can’t quote messages on the mobile website.<\/p>","upvoteCount":1,"datePublished":"2014-07-10T13:31:50.000Z","url":"https://community.spiceworks.com/t/powershell-to-import-into-ad/320026/11","author":{"@type":"Person","name":"chamele0n","url":"https://community.spiceworks.com/u/chamele0n"}},{"@type":"Answer","text":"
I finally got it working with the following code:<\/p>\n
Import-Module ActiveDirectory\n$users = Import-CSV \"C:\\aduserupdate.csv\"\n$SAM = $_.sAMAccountName\n$title = $_.title\n$department = $_.department\n$description = $_.description\n$mail = $_.EmailAddress\n$mobile = $_.MobilePhone\n$company = $_.Company\n$ipPhone = $_.OfficePhone\nforeach ($user in $users) {\nGet-ADUser -Filter \"SamAccountName -eq '$($user.samaccountname)'\" -Properties * -SearchBase \"ou=users,dc=internal,dc=fake,dc=com\"\nSet-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)\n}\n<\/code><\/pre>\nAlso 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!<\/p>","upvoteCount":0,"datePublished":"2014-07-10T13:34:52.000Z","url":"https://community.spiceworks.com/t/powershell-to-import-into-ad/320026/12","author":{"@type":"Person","name":"valentinarroyo8372","url":"https://community.spiceworks.com/u/valentinarroyo8372"}},{"@type":"Answer","text":"
Import-Module ActiveDirectory\n$users = Import-CSV \"C:\\aduserupdate.csv\"\nforeach ($user in $users) {\nGet-ADUser -Filter \"SamAccountName -eq '$($user.samaccountname)'\" -Properties * -SearchBase \"ou=builders,dc=internal,dc=fake,dc=com\"\nSet-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)\n}\n<\/code><\/pre>\nHere’s the cleaned up code without all the variables. Thanks again for everyone’s help<\/p>","upvoteCount":0,"datePublished":"2014-07-10T13:43:10.000Z","url":"https://community.spiceworks.com/t/powershell-to-import-into-ad/320026/13","author":{"@type":"Person","name":"valentinarroyo8372","url":"https://community.spiceworks.com/u/valentinarroyo8372"}},{"@type":"Answer","text":"
Why are you running get-aduser? It’s not doing anything other than reporting back user information.<\/p>","upvoteCount":1,"datePublished":"2014-07-10T13:55:39.000Z","url":"https://community.spiceworks.com/t/powershell-to-import-into-ad/320026/14","author":{"@type":"Person","name":"Rob-Dunn","url":"https://community.spiceworks.com/u/Rob-Dunn"}},{"@type":"Answer","text":"
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 :).<\/p>","upvoteCount":0,"datePublished":"2014-07-10T13:59:56.000Z","url":"https://community.spiceworks.com/t/powershell-to-import-into-ad/320026/15","author":{"@type":"Person","name":"valentinarroyo8372","url":"https://community.spiceworks.com/u/valentinarroyo8372"}},{"@type":"Answer","text":"
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…!<\/p>","upvoteCount":0,"datePublished":"2014-07-10T14:01:19.000Z","url":"https://community.spiceworks.com/t/powershell-to-import-into-ad/320026/16","author":{"@type":"Person","name":"Rob-Dunn","url":"https://community.spiceworks.com/u/Rob-Dunn"}},{"@type":"Answer","text":"
<\/strong>I’m not sure why it worked for you and not for me… then again, I dont know much about powershell.<\/p>\nSorry! I didn’t see those messages above my solution, must have needed to refresh.<\/p>\n
Version: Major = 3 Minor = 0 Build = -1 Revision = -1<\/p>","upvoteCount":0,"datePublished":"2014-07-10T14:03:59.000Z","url":"https://community.spiceworks.com/t/powershell-to-import-into-ad/320026/17","author":{"@type":"Person","name":"valentinarroyo8372","url":"https://community.spiceworks.com/u/valentinarroyo8372"}},{"@type":"Answer","text":"
And then just for ease of reading the one-liner, you could separate each row with the backtick ( ` ) symbol like so:<\/p>\n
Import-Module ActiveDirectory\nImport-CSV c:\\aduserupdate.csv | Foreach-Object{\n\tSet-ADUser -Identity $_.sAMAccountName `\n\t-Company $_.Company `\n\t-Department $_.department `\n\t-Description $_.description `\n\t-EmailAddress $_.EmailAddress `\n\t-MobilePhone $_.MobilePhone `\n\t-Office $company `\n\t-OfficePhone $_.OfficePhone `\n\t-Title $_.title\n}\n<\/code><\/pre>","upvoteCount":3,"datePublished":"2014-07-10T14:37:25.000Z","url":"https://community.spiceworks.com/t/powershell-to-import-into-ad/320026/19","author":{"@type":"Person","name":"chamele0n","url":"https://community.spiceworks.com/u/chamele0n"}},{"@type":"Answer","text":"Cool thanks Chamele0n!<\/p>","upvoteCount":0,"datePublished":"2014-07-10T14:59:58.000Z","url":"https://community.spiceworks.com/t/powershell-to-import-into-ad/320026/20","author":{"@type":"Person","name":"valentinarroyo8372","url":"https://community.spiceworks.com/u/valentinarroyo8372"}}]}}
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}
Rob-Dunn
(Rob Dunn)
July 10, 2014, 11:03am
3
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
Rob-Dunn
(Rob Dunn)
July 10, 2014, 11:06am
4
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: ( [Set-ADUser], ParameterBindingValidationException
FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser
Rob-Dunn
(Rob Dunn)
July 10, 2014, 11:30am
7
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.
Rob-Dunn
(Rob Dunn)
July 10, 2014, 1:23pm
9
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?
Rob-Dunn
(Rob Dunn)
July 10, 2014, 1:24pm
10
Additionally, put this line above set-aduser:
get-variable | out-string
Do you see $SAM being populated when you run it?
chamele0n
(Chamele0n)
July 10, 2014, 1:31pm
11
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
Rob-Dunn
(Rob Dunn)
July 10, 2014, 1:55pm
14
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 :).
Rob-Dunn
(Rob Dunn)
July 10, 2014, 2:01pm
16
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
chamele0n
(Chamele0n)
July 10, 2014, 2:28pm
18
Because in this post:
Valentin Arroyo:
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: ( [Set-ADUser], ParameterBindingValidationException
FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser
You did not have the Import-CSV piping to For-Each like I mentioned in this Post:
Chamele0n:
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.
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.
chamele0n
(Chamele0n)
July 10, 2014, 2:37pm
19
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