Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

Use PS cmdlet to read a CSV file and update AD attributes

I have a big CSV file which contains rows and rows of information like the following:

Samaccountname, givenname, surname, telephonenumber, mail.

Please advise a PS cmdlet that reads the CSV file and update user attribute fields in AD.

Thanks.
0
nav2567
Asked:
nav2567
  • 3
  • 2
2 Solutions
 
David Johnson, CD, MVPOwnerCommented:
#Samaccountname, givenname, surname, telephonenumber, mail
$users = import-csv c:\test\users.csv
foreach ($user in $users){
get-aduser -filter samaccountname -eq $user.samaccountname | 
    set-aduser -Surname $user.surname -GivenName $user.givenname `
        -EmailAddress $user.mail -OfficePhone $user.telephonenumber
}

Open in new window

0
 
nav2567Author Commented:
Thanks.  

I am getting the following error when I run the cmdlets:

Get-ADUser : A parameter cannot be found that matches parameter name 'eq'.
At line:1 char:64
+ foreach ($user in $users){get-aduser -filter samaccountname -eq <<<<  $user.samaccountname |set-aduser -Surname $use
.surname - GivenName $user.givenname -OfficePhone $user.telephonenumber
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADUser

Also, would you please let me know what exactly should the content of my CSV file contain?

Currently, it is like this:

jsmith John Smith 213-334-5566
mjane Mary Jane 213-223-4455
mmecay Mike Meccay 208-334-5566
...
0
 
QlemoC++ DeveloperCommented:
nav2567, you need to be exact in what you provide. The data in http:#a40519563 is no CSV format, and in the original question you showed a (proper) CSV header line. The CSV should look like this:
Samaccountname, givenname, surname, telephonenumber, mail
jsmith, John, Smith, 213-334-5566,
mjane, Mary, Jane, 213-223-4455,
mmecay, Mike, Meccay, 208-334-5566,

Open in new window

Since you have not provided an email address, the cmdlet will fail. I've hence removed that from the command:
import-csv c:\test\users.csv | % {
  set-aduser $_.SamAccountName -Surname $_.surname -GivenName $_.givenname -OfficePhone $_.telephonenumber
}

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
nav2567Author Commented:
Sorry. Let me try that again.

Thanks for your feedback.
0
 
nav2567Author Commented:
Thanks, everyone.
0
 
QlemoC++ DeveloperCommented:
Since my solution works, and the other one does not and is overly complicated, I would have expected to receive at least the majority of points.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now