• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 776
  • Last Modified:

Export Specific AD Fields for Users in OU

I need to export all users from two separate OU'S in AD to a CSV but with only specific fields.

So I need an export of all users in OU=MCM,OU=LON,OU=EMEA,DC=ad,DC=domainname,DC=net
and also OU=GRM,OU=LON,OU=EMEA,DC=ad,DC=insidemedia,DC=net

to the same CSV and i would like only the following fields to be exported.

FirstName:LastName:City:Street:Company:Email:TelephoneNumber:Pre2kName:Domain\Pre2kName:


How can I get this in a script that can be automated.

Many thanks
0
regnighc
Asked:
regnighc
  • 4
  • 3
  • 2
2 Solutions
 
Adam BrownSr Solutions ArchitectCommented:
If you can use the Quest Powershell Cmdlets (available here: http://www.quest.com/powershell/activeroles-server.aspx )
you can use the attached command to export it all.
get-qaduser -searchbase ad.domainname.net/emea/lon/mcm | select firstname,lastname,city,streetaddress,company,mail,phonenumber,samaccountname,ntaccountname | export-csv c:\users.csv

Open in new window

0
 
Adam BrownSr Solutions ArchitectCommented:
Sorry. Small typo in that one. Attached is correct.
get-qaduser -searchroot ad.domainname.net/emea/lon/mcm | select firstname,lastname,city,streetaddress,company,mail,phonenumber,samaccountname,ntaccountname | export-csv c:\users.csv

Open in new window

0
 
regnighcAuthor Commented:
Can I adapt this to export from two containers to the one csv?
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
Adam BrownSr Solutions ArchitectCommented:
You can, but it's a little trickier, since the default export-csv cmdlet doesn't allow appending data. You'd do something like the attached.

"firstname,lastname,city,streetaddress,company,mail,phonenumber,samaccountname,ntaccountname" > c:\user.csv
get-qaduser -searchroot domain.com/ou1 | foreach {$firstname,$lastname,$city,$streetaddress,$company,$mail,$phonenumber,$samaccountname,$ntaccountname = $_.firstname,$_.lastname,$_.city,$_.streetaddress,$_.company,$_.mail,$_.phonenumber,$_.samaccountname,$_.ntaccountname
"$firstname,$lastname,$city,$streetaddress,$company,$mail,$phonenumber,$samaccountname,$ntaccountname" >> c:\users.csv}
get-qaduser -searchroot domain.com/ou2 | foreach {$firstname,$lastname,$city,$streetaddress,$company,$mail,$phonenumber,$samaccountname,$ntaccountname = $_.firstname,$_.lastname,$_.city,$_.streetaddress,$_.company,$_.mail,$_.phonenumber,$_.samaccountname,$_.ntaccountname 
"$firstname,$lastname,$city,$streetaddress,$company,$mail,$phonenumber,$samaccountname,$ntaccountname" >> c:\users.csv}

Open in new window

0
 
regnighcAuthor Commented:
Also do you know how I can pass this script as a parameter to the Cmdlets shell so that I can schedule it?
0
 
Adam BrownSr Solutions ArchitectCommented:
That can be a little tricky...Save the code as a .ps1 file, then you need to create a Console file for powershell. While the Quest Active Roles console is open, run export-console c:\powershell.psc1

The command you would use is attached.
powershell -console c:\powershell.psc1 -command "& 'script.ps1' "

Open in new window

0
 
Chris DentPowerShell DeveloperCommented:
acbrown2010,

> You can, but it's a little trickier

You're making that too complex for yourself, it doesn't need to be. Get-QADUser will quite happily take a list to search, but even if it didn't you could loop and stick with a single output pipeline. Do that and you can keep Export-Csv as the original simple script.
Get-QADUser -SearchRoot "domain.com/ou1", "domain.com/ou2" | 
  Select-Object FirstName, LastName, City, StreetAddress, Company, Mail, PhoneNumber, SamAccountName, NTAccountName |
  Export-Csv "C:\User.csv" -NoTypeInformation

Open in new window

Cheers,

Chris
0
 
Chris DentPowerShell DeveloperCommented:
This is here as an example only. It's unnecessary, it only demonstrates preservation of a single output pipeline:
"ou1", "ou2" | ForEach-Object {
  Get-QADUser -SearchRoot "domain.com/$_"
} | Select-Object Name | Export-Csv "SomeFile.csv" -NoTypeInformation

Open in new window

Manually writing output is really not at all fun, and this example should not be used, the version above is far better, demonstrative only.

Chris
0
 
Chris DentPowerShell DeveloperCommented:
heh one more:

> While the Quest Active Roles console is open, run export-console c:\powershell.psc1

Make the first line in the script this:

Add-PsSnapIn Quest.ActiveRoles.ADManagement

Then you can drop the complexity of creating a console. That's all the important work the console really does :)

Chris
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now