HTML defines a number of special characters; for example, a literal ampersand (&) in HTML must be written as &.
ConvertTo-Html will handle the conversion of special characters in input objects, but it will not work with special characters in raw HTML that are added using the Body, Head, PreContent, or PostContent parameters.
The Sytem.Web.HttpUtility class includes methods that are able to convert strings containing such characters.
Before System.Web.HttpUtility can be used, the assembly must be added:
Add-Type -AssemblyName System.Web
The HtmlEncode static method will take a string and replace any reserved characters with HTML code. For example, the following snippet will replace > with >:
PS>'<h1>{0}</h1>' -f [System.Web.HttpUtility]::HtmlEncode('Files > 100MB') <h1>Files > 100MB</h1>
The HtmlDecode static method can be used to reverse the process:
PS> [System.Web.HttpUtility]::HtmlDecode("<h1>Files > 100MB</h1>")
<h1>Files > 100MB</h1>