Export LDAP information to CSV with powershell

Hello,

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
"Name","PostOfficeBox"
"System.DirectoryServices.ResultPropertyValueCollection","System.DirectoryServices.ResultPropertyValueCollection"

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.
SybuxAsked:
Who is Participating?
 
Jamie McKillopIT ManagerCommented:
Hello,

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


JJ
0
 
rwskasCommented:
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
...
etc

Will resolve your CSV issue.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.