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 static method HtmlEncode will take a string and replace any reserved characters with an 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 static method HtmlDecode can be used to reverse the process:
PS> [System.Web.HttpUtility]::HtmlDecode("<h1>Files > 100MB</h1>")
<h1>Files > 100MB</h1>