Solved

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

Posted on 2013-01-17
13
476 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 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