title: Powershell Hash Table to HTML
body: <p>I am trying to write a very simple Powershell script that checks files in a location and then decides if any are older than 90 days. Easy but it would be good if I can use PS's send-mailmessage cmdlet to use the hash table I have created and accept the contents formatted into HTML to be use in inside a pre build email template.</p>
  
  <p>I have put together the following script (with the table part lifted from another post on StackOverflow - <a href="http://stackoverflow.com/questions/11263538/powershell-display-table-in-html-email">http://stackoverflow.com/questions/11263538/powershell-display-table-in-html-email</a>):</p>
  
  <pre><code>$ErrorActionPreference = "SilentlyContinue"
  $Path = "Z:\dokuwiki-20140505-0\apps\dokuwiki\data\pages"
  $Days = 90
  $Now = Get-Date
  $ExePath="TBC"
  
  # Set up Functions
  
  function MailSend
  {
      param([string]$message,[string]$days,[string]$html)
      #$message and $html are called inside a HTML format file called Send-Email.html
      try 
      {
          # Read html test file and interpolate the variables
          $body = iex('"' + (Get-Content "$ExePath\Send-Email.html") + '"')
          # Set mail message parameters
          $messageParameters = @{
              SmtpServer = "smtpserver"
              From = "sender"
              To = "recipient1"
              Cc = "recipient2"
              Subject = "DokuWiki files have not been modified in $days" 
              body = $body
              }
  
          Send-MailMessage @messageParameters -BodyAsHtml
      }
      catch
      {
          Write-Warning "Unable to send email alert: $_"      
      }
  } #MailSend
  
  # Get list of files to transfer
  $FileList = Get-ChildItem  -Path $Path -Recurse | Where-Object { $_.mode -notmatch 'd'}         
  
  if ($FileList -eq $null) 
  {
      # No files to process
      Write-Host "No files found - This is probably a problem"
      exit
  }
  
  # Find the objects available using Select-Object
  #$FileList | Select-Object
  
  # Check there are files older than $Days
  foreach ($File in $FileList)
  {
      #Write-Host $File.Name
      $MTime=$File.LastWriteTime
      if(($Now - $MTime).Days -ge $Days)
      {
          $matches.Add($File.Directory,$File)     
      } 
  }
  
  # Create a DataTable
  $table = New-Object system.Data.DataTable "AgedFiles"
  $col1 = New-Object system.Data.DataColumn Path,([string])
  $col2 = New-Object system.Data.DataColumn File,([string])
  $table.columns.add($col1)
  $table.columns.add($col2)
  
  #How do I populate the table?
  
  #How to I get it into HTML format.
  
  # Create an HTML version of the DataTable
  $html = "&lt;table&gt;&lt;tr&gt;&lt;td&gt;Path&lt;/td&gt;&lt;td&gt;Table&lt;/td&gt;&lt;/tr&gt;"
  foreach ($row in $table)
  { 
      $html += "&lt;tr&gt;&lt;td&gt;" + $row[0] + "&lt;/td&gt;&lt;td&gt;" + $row[1] + "&lt;/td&gt;&lt;/tr&gt;"
  }
  $html += "&lt;/table&gt;"
  
  #This section prints out the Hash Array to STD-OUT and is to be removed
  foreach($item in $matches.GetEnumerator() | Sort-Object Value)
  {
      if ($item.Value -ne "d")
      {
          write-host "$($item.name)\$($item.Value)"
      }
  }
  
  MailSend "This is the message that will appear inside the email body" $days $html
  
  exit
  </code></pre>
  
  <p>I am new to Powershell but have experience with Bash and Perl plus a few others but I just can't see how to do this one. </p>
  
  <p>Any ideas would be greatly appreciated.</p>