How to render clickable link using Powershell

15,129

Solution 1

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)

Solution 2

i.e.

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

Add-Type -AssemblyName System.Web

[System.Web.HttpUtility]::HtmlDecode($pso) | 
Out-File \\myfolder\InventoryReports\$CurrentMonth.html
Share:
15,129
Nish
Author by

Nish

English is not my first language.

Updated on June 14, 2022

Comments

  • Nish
    Nish almost 2 years

    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

  • Nish
    Nish about 11 years
    where should i add this Add-Type -AssemblyName System.Web [System.Web.HttpUtility]::HtmlDecode($pso) at the starting of the script ?
  • Shay Levy
    Shay Levy about 11 years
    Yes, or just before you first use it.