How to render clickable link using Powershell

Nish picture Nish · Mar 22, 2013 · Viewed 14.4k times · Source

I am using below powershell script to write the output to a html file, the issue is its rendering everything as text.

My code:

$PagesObject = New-Object PSObject
Add-Member -input $PagesObject noteproperty 'Site Url' $spweb.url
Add-Member -input $PagesObject noteproperty 'Page Title' $PageTitle
Add-Member -input $PagesObject noteproperty 'Page Url' "<a href="$PagesUrl">$PageTitle</a>"     
$results += $PagesObject
$results | ConvertTo-HTML -head  $a  -body | Out-File \\myfolder\InventoryReports\$CurrentMonth.html

I want to render it as clickable link

check below screenshot to see how it is rendering now enter image description here

Thanks in advance

Answer

Shay Levy picture Shay Levy · Mar 23, 2013

PowerShell encodes special characters. One way to solve this would be to convert them back and then write the result ot a file:

$pso = New-Object PSObject -Property @{
    SiteUrl = $spweb.url
    PageTitle= $PageTitle
    PageUrl = "<a href='$PagesUrl'>$PageTitle</a>"  
} | ConvertTo-Html


$pso -replace '&gt;','>' -replace '&lt;','<' -replace '&#39;',"'" | 
Out-File \\myfolder\InventoryReports\$CurrentMonth.html

UPDATE:

Here's a better way that will take care of all html entities:

Add-Type -AssemblyName System.Web
[System.Web.HttpUtility]::HtmlDecode($pso)