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

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?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
$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; $objItem.name}
 cls;Write-Host "Finished..."
Open in New Window Select All

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

That was harder than I expected.. try this:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
$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 Select All
Random Solutions  
 
programming4us programming4us