Solved

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

Posted on 2013-01-17
13
477 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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 40

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 40

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
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.

742 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