[Last Call] Learn how to a build a cloud-first strategyRegister Now


Export LDAP information to CSV with powershell

Posted on 2012-09-14
Medium Priority
Last Modified: 2012-09-14

I'm trying to create a little script to export some information of my ldap to a CSV file.

I'm using this little script :
$strFilter = "((objectCategory=User))"

$objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://OU=Utilisateurs,DC=myldap")
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 10
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"

$colProplist = "name","postofficebox"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

$colResults = $objSearcher.FindAll()

$myList = @()
foreach ($objResult in $colResults) {
    $objItem = $objResult.Properties
    $myUSer = "" | Select-Object Name, PostOfficeBox
    $myUser.Name = $objResult.Properties["name"]
    $myUser.PostOfficeBox = $objResult.Properties["postofficebox"]
    $myList += $myUser

Open in new window

but when I export $mylist to csv, I get only the object type and not the value as you can see :
#TYPE Selected.System.String

Open in new window

I running it for a quite long time and I can get out where I'm wrong.
Any help should be very welcome.
Question by:Sybux
LVL 37

Accepted Solution

Jamie McKillop earned 1000 total points
ID: 38399808

Download the free Quest cmdlets and you can replace your script with one line:

Get-QADUser -SizeLimit 0 | select name,postofficebox | Export-CSV c:\users.csv -NoTypeInformation

Open in new window


Assisted Solution

rwskas earned 1000 total points
ID: 38400013
To maintain using your same script, use Out-String on the property lines.

So something like...

$myUSer = "" | Select-Object Name, PostOfficeBox
$myUser.Name = $ObjResult.Properties.Name | Out-String

Will resolve your CSV issue.

Featured Post

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.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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 anti-spam), the admin…
Loops Section Overview

829 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