output of a collection using powershell and htmlAgilityPack

html-agility-pack powershell powershell-v2.0

Question

First, yes I am a noob to scripting and powershell as that will become painfully obvious. In my attempts to learn, I have been working with a script from here: http://www.leeholmes.com/blog/2010/03/05/html-agility-pack-rocks-your-screen-scraping-world/

This is what I have thus far:

add-type -Path 'C:\Program Files (x86)\htmlAgilityPack\HtmlAgilityPack.dll'
$doc = New-Object HtmlAgilityPack.HtmlDocument 
$result = $doc.Load("C:\scipts\test.html") 
$texts = $doc.DocumentNode.SelectNodes("//table[@class='dataTable']/tbody/tr/td[1]/a[1]") 
$result = $texts | % {
$testText = $_
$Platform = $testtext.innertext
New-Object PsObject -Property @{ Platform = $Platform} | Select Platform 
}
$result | Sort Platform | out-string

This gives me the items I am looking for.

* The Challenge *

I am trying to get that output into a variable as a single string with each item followed by a comma. for Example Item1, Item2, Item 3 etc...

Any help or explanation would be appreciated as I have Googled myself cross-eyed and don't necessarily stand the things I am finding.

Thanks!

Accepted Answer

From the powershell help (get-help about_join):

The following diagram shows the syntax for the join operator.

<String[]> -Join <Delimiter>

To solve your problem, you need to:

  1. Create an array of strings containing the strings you want to join:

    $arrayofstrings = $result | Sort Platform | foreach-object {$_.Platform}
    
  2. Join the strings using a comma as the delimiter:

    $joinedstring = $arrayofstrings -join ", "
    

$joinedstringwill contain Item1, Item2, Item3




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why