Solved

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

Posted on 2013-01-17
13
472 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Shouldn't all users have the same email signature?

You wouldn't let your users design their own business cards, would you? So, why do you let them design their own email signatures? Think of the damage they could be doing to your brand reputation! Choose the easy way to manage set up and add email signatures for all users.

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…
Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

705 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

14 Experts available now in Live!

Get 1:1 Help Now