Export LDAP information to CSV with powershell

Posted on 2012-09-14
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


    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

    LVL 6

    Assisted Solution

    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

    Highfive Gives IT Their Time Back

    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!

    Join & Write a Comment

    Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
    "Migrate" an SMTP relay receive connector to a new server using info from an old server.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    734 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

    22 Experts available now in Live!

    Get 1:1 Help Now