Solved

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

Posted on 2014-12-26
6
206 Views
Last Modified: 2014-12-29
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
Comment
Question by:nav2567
  • 3
  • 2
6 Comments
 
LVL 79

Accepted Solution

by:
David Johnson, CD, MVP earned 400 total points
ID: 40518731
#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
 

Author Comment

by:nav2567
ID: 40519563
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
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 100 total points
ID: 40520510
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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:nav2567
ID: 40521209
Sorry. Let me try that again.

Thanks for your feedback.
0
 

Author Closing Comment

by:nav2567
ID: 40521277
Thanks, everyone.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40521435
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
A brief introduction to what I consider to be the best editor for PowerShell.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question