You need to quote the values you are retrieving from the cells.
Something like this should work:
# load the System.Web assembly to be able to decode HTML entities
Add-Type -AssemblyName System.Web
$result = Invoke-WebRequest 'https://htmlwithtable.htm'
$data = $result.ParsedHtml.getElementsByTagName("table") | Select-Object -First 1
$table = $data.Rows | ForEach-Object {
if ($_.tagName -eq 'tr'){
$csvRow = foreach($cell in $_.children){
if ($cell.tagName -match 't[dh]'){
# decode HTML entities and double-up quotes that the value may contain
$value = [System.Web.HttpUtility]::HtmlDecode($cell.innerText) -replace '"', '""'
if ($cell.innerHtml -match 'href="([^"]*)') {
# if the cell contains a link, add it to the value between brackets
$value += ' ({0})' -f $Matches[1]
}
'"{0}"' -f $value
}
}
$csvRow -join ','
}
}
$table | Out-File 'c:\change\htmltocsv.csv'