Solved

Powershell: How to export users from AD into a CSV then import it back into AD

Posted on 2013-01-17
13
475 Views
Last Modified: 2013-01-22
Hello,

I did submit the following question stated on the link but is more relevant to powershell than  Active Directory..

http://www.experts-exchange.com/Software/Server_Software/File_Servers/Active_Directory/Q_27997464.html

Thanks for your help,
0
Comment
Question by:namerg
  • 8
  • 3
  • 2
13 Comments
 
LVL 31

Expert Comment

by:Henrik Johansson
ID: 38787239
Export:
get-aduser -filter "samaccountname -like 'a*'" -properties givenName,sn,Department| export-csv filename.csv

Open in new window


Import changes:
import-csv filename.csv |foreach
{
  set-aduser $_.samaccountname -givenName $_.givenName -surname $_.sn -department $_.department
} 

Open in new window

0
 

Author Comment

by:namerg
ID: 38787501
@henjoh09, what about if the export comes from a certain OU ?
0
 
LVL 31

Assisted Solution

by:Henrik Johansson
Henrik Johansson earned 500 total points
ID: 38787544
If you want to export a specific OU, you can add -SearchBase parameter to Get-ADUser cmdlet.

-SearchBase 'OU=child,OU=parent,DC=domain,DC=local'

Open in new window

0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:namerg
ID: 38787562
Like this:
get-aduser -SearchBase 'OU=child,OU=parent,DC=domain,DC=local' -filter "samaccountname -like 'a*'" -properties givenName,sn,Department| export-csv filename.csv

Open in new window

0
 

Author Comment

by:namerg
ID: 38787809
Hmm, I am getting the CSV blank. :(
0
 

Author Comment

by:namerg
ID: 38788086
When i execute
Get-ADUser -SearchBase "OU=OU,DC=DOMAIN,DC=COM" -filter {objectClass -eq "user"} -properties sn,givenName,title,department,employeeid,employeenumber,samaccountname| export-csv TEST.csv

Open in new window


I get the following wrong headers:
"Department","DistinguishedName","EmployeeID","EmployeeNumber","Enabled","GivenName","Name","ObjectClass","ObjectGUID","SamAccountName","SID","Surname","Title","UserPrincipalName"

I want the ones I specified in -properties

Thanks for your help,
0
 

Author Comment

by:namerg
ID: 38788119
Guru subsun, or footech...I am crying for your help. ;(
0
 

Accepted Solution

by:
namerg earned 0 total points
ID: 38788252
Never mind, I got it.

I used
Get-ADUser -filter * -Properties sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber,sAMAccountName  -SearchBase "OU=UPI,DC=domain,DC=com" | Select-Object sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber,sAMAccountName | export-csv TEST.csv -notype

Open in new window

0
 
LVL 39

Expert Comment

by:footech
ID: 38789316
@namerg - In reply to your comment "...but never got a reply when I was crying for help".  Sorry, but unless you've waited at least a day for a reply there's no reason to complain.  We're all volunteers here and the times we're available can be unpredictable.  You were right to award the points as you did.
0
 

Author Comment

by:namerg
ID: 38789343
Thanks footech for your email. Sorry. Hey, about the other question that you helped me I am going to split points between you and subsun, is that ok ?
0
 
LVL 39

Expert Comment

by:footech
ID: 38789759
That's fine.  No need to ask.  If I ever feel strongly about how points are awarded I'll object, but that rarely happens.
0
 
LVL 31

Expert Comment

by:Henrik Johansson
ID: 38789809
As question also asked for importing the changes back, I think you should had included my original comment instead of the followup as answer.
I should had read the linked question thread more careful as it contains some followup comments about what properties you wanted to change/add and could based on that maybe have given you a more complete script from the beginning.
Maybe not so good idea to link to a question you've requested to delete as the reference will be broken... :) About the delete request comment, it had been better to use "request attention"-button to ask moderators to change zone for question.

However, glad to help by pointing in correct direction./Henrik
0
 

Author Closing Comment

by:namerg
ID: 38804797
Well, henjoh09 gave me the hint but never got a reply when I was crying for help. No hard feelings if you do not accept my answer as solution.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create scheduled tasks in windows 10 via GPO 5 33
User account lockout - Server 2012R2 7 30
Lync 2010 4 32
Old Active Directory sync to Azure 3 14
OfficeMate Freezes on login or does not load after login credentials are input.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

825 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