title: Exporting PowerShell Foreach Loop to HTML
body: <p>I've been trying to write a script that pings a list of computers from a text file and exports the output to a HTML file. </p>
  
  <p>Using a ForEach loop and and if/else statement I have been able to get a working ping script working that displays in PowerShell but haven't been able to export the results to a html file. </p>
  
  <p>When I run the script the HTML file opens but only displays the line "Here are the ping results for $date"</p>
  
  <p>I'm pretty new to PowerShell so any kind of input or help would be appreciated!</p>
  
  <pre><code>$ComputersAry = Get-Content -Path "C:\Script\ping.txt"
  $filepath = "C:\Script\"
  $date = "{0:yyy_MM_dd-HH_mm}" -f (get-date)
  $file = $filepath + "Results_" + $date + ".htm"
  New-Item $filepath -type directory -force -Verbose 
  
  $Header = @"
  &lt;style&gt;
  TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
  TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
  TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
  &lt;/style&gt;
  &lt;title&gt;
  LRS Ping Results
  &lt;/title&gt;
  "@
  
  Foreach ($MachineName in $ComputersAry) { 
  
      $PingStatus = Gwmi Win32_PingStatus -Filter "Address ='$MachineName'" | Select-Object StatusCode
  
          if($PingStatus.StatusCode -eq 0){
          $output = write-host "$MachineName,Ping Success!!,$Date" 
          } else {
          $output = write-host "$MachineName,Ping FAIL, please investigate cause ASAP!!" 
          } 
  
      }
  
  
  $pre= "Here are the ping results for $date"
  
  $output | Select-Object Name, Status, Date | ConvertTo-HTML -Head $Header -PreContent $pre | Out-File $file
  
  Invoke-Item $file
  </code></pre>