How do I Pipe Value of PowerShell Array Value to Export-Csv?

Posted on 2009-05-01
Last Modified: 2012-05-06
I use the script posted in the code section of this post to return information from Active Directory.

What I would like to know is how I can pipe the results of the $colResults variable to a csv without it giving me the LDAP and properties of the System.DirectoryServices.DirectoryEntry object. What I'm after is a comma separated values machine list. The last 2 lines of code will give me exactly what I want, but it writes it to the host.

I have tried repositioning the pipe line for the Export-csv call, but it either prompts me for the input object for each machine, or else if I put it at the end, it returns an empty file with just a length header and a numeric value representing the length of the last object passed, which I assume to be a machine name.

Any idea what I'm doing wrong?
$strFilter = "(&(objectCategory=Computer)(Name=FBX*))"

$objDomain = New-Object System.DirectoryServices.DirectoryEntry

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher

$objSearcher.SearchRoot = $objDomain

$objSearcher.PageSize = 1000

$objSearcher.Filter = $strFilter

$objSearcher.SearchScope = "Subtree"

$colProplist = "name"

foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

$colResults = $objSearcher.FindAll()|Export-Csv -NoTypeInformation "C:\Documents and Settings\tlafferty\Desktop\comps.csv"

#foreach ($objResult in $colResults)

 #   {$objItem = $objResult.Properties; $}

 cls;Write-Host "Finished..."

Open in new window

Question by:goneal
    LVL 4

    Accepted Solution

    That was harder than I expected.. try this:

    $strFilter = "(&(objectCategory=Computer)(Name=FBX*))"
    $objDomain = New-Object System.DirectoryServices.DirectoryEntry
    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = $objDomain
    $objSearcher.PageSize = 1000
    $objSearcher.Filter = $strFilter
    $objSearcher.SearchScope = "Subtree"
    $colProplist = "name"
    foreach ($i in $colPropList){[void]$objSearcher.PropertiesToLoad.Add($i)}
    $colResults = $objSearcher.FindAll()
    $colResults | %{ $a = New-Object PSObject ; $a | Add-Member -MemberType NoteProperty -Name Name -Value $_.Properties.Item("Name")[0] ; $a } | Export-Csv -NoTypeInformation "C:\Documents and Settings\tlafferty\Desktop\comps.csv"

    Open in new window

    LVL 1

    Author Closing Comment

    This was right on the money first try! Thanks so much for your help.
    LVL 1

    Author Comment

    Very useful! One thing that was not clear is what the [void] on line 12 accomplishes aside from the obvious (voiding...) or why it was needed. Could you explain? I'm a bit of a PowerShell novice.

    Also, could you explain the last line of code? I understand that you are establishing a new variable $a as an object to contain the output of $colResults, but I'm not sure what else is happening. Would you mind a blow-by-blow?

    In the meantime, thanks again. It was right on the first attempt.
    LVL 70

    Expert Comment

    by:Chris Dent

    PropertiesToLoad.Add returns a value, a count of properties. It's not very interesting, Voiding that is one way of dropping the value so it doesn't appear when the code runs. Otherways include redirecting the output to Null, or assigning it to a variable (say $Return) and ignoring it.

    For the sake of discussion, I would take a few short cuts in the code above :)

    Using $Null in place of the directory entry ($objDomain) will have it use the default value, the current domain.

    The Filter can be passed in as a parameter when creating the Directory Searcher.

    The SearchScope defaults to subtree so has been removed from the code (if you want to see that, just run "$DirSearcher" on its own with the example below, it'll show you the values set).

    Using PropertiesToLoad.AddRange saves us a loop through each properties. AddRange doesn't return a value either so there's no need to Void it or deal with the returned value. It needs an array as the parameter, which is why $PropList is @("value1", "value2") now.

    Using Select-Object with a custom value allows us to skip creation of a new object and separate addition of a property to that object. It also loses the other loop.

    Finally, you may also consider grabbing these:

    Because those will shorten your code to this:

    Get-QADComputer -Name "FBX*" | Select-Object Name | Export-CSV "somefile.csv"

    PowerShell is fun ;)

    $LdapFilter = "(&(objectCategory=Computer)(name=FBX*))"
    $DirSearcher = New-Object System.DirectoryServices.DirectorySearcher($Null, $LdapFilter)
    $DirSearcher.PageSize = 1000
    $PropList = @("name")
    $DirSearcher.FindAll() | `
      Select-Object @{n='Name';e={ $_.Properties["name"] }} | `
      Export-Csv -NoTypeInformation "C:\Documents and Settings\tlafferty\Desktop\comps.csv"

    Open in new window

    LVL 4

    Expert Comment

    Chris is correct on the use of void.

    $a = New-Object PSObject
    - this creates a new object, the idea is to create a rich object with properties, and this provides the base object for us to add properties to.

    $a | Add-Member -MemberType NoteProperty -Name Name -Value $_.Properties.Item("Name")[0]
    - this adds a property to $a called Name with the value of $_.Properties.Item("Name")[0]

    - this outputs the object we just created to the pipeline so it is passed to Export-Csv

    I'm probably not explaining this great, and I didn't find any really good references out there either. Have a look at this and see if you can make sense of it:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    In this previous article (, we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
    The viewer will learn how to dynamically set the form action using jQuery.

    759 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

    12 Experts available now in Live!

    Get 1:1 Help Now