Solved

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

Posted on 2014-12-26
6
203 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 78

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 68

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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 68

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Set OWA language and time zone in Exchange for individuals, all users or per database.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now