title: Escape an entire string variable which MAY contain characters that need escaping
body: <p>My PowerShell V3 creates an Office 365 user based on a series of questions, and then sends an email to the IT team with the details. The problematic bit is:</p>
  
  <pre><code>#Generate-RandomPassword
  $xpassword = Create-ComplexPassword -PasswordLength 14 -LowerAlphas 1 -UpperAlphas 1 -Numbers 1 -SpecialCharacters 1
  Add-Type -AssemblyName System.Web
  [System.Web.HttpUtility]::HtmlEncode($xpassword)
  
  
  Write-Host 'Sending Email' -foregroundcolor yellow
  $xbody =  "&lt;head&gt;"
  $xbody += "&lt;style&gt;"
  $xbody += "p{"
  $xbody += "font-family:Verdana;"
  $xbody += "font-size:10pt;"
  $xbody += "margin-top:0;"
  $xbody += "margin-bottom:0;"
  $xbody += "}"
  $xbody += "&lt;/style&gt;"
  $xbody += "&lt;/head&gt;"
  $xbody += "&lt;p&gt;Password: &lt;span style=`"font-family:'Lucida Console',sans serif;font-size:10pt;color:red`"&gt;$xpassword&lt;/span&gt;"
  $xbody += "&lt;p&gt;License: $xk1ore1"
  $xbody += "&lt;p&gt;Country: $xcountry"
  $xbody += "&lt;p&gt;Town: $xtown"
  $xbody += "&lt;p&gt;Status: $xstatus"
  $xbody += "&lt;p&gt;Laptop User: $xlaptopuser"
  $xbody += "&lt;p&gt;CRM User: $xcrm"
  $xbody += "&lt;p&gt;Operations Mailbox Access: $xoperations"
  $xbody += "&lt;p&gt;Finance Mailbox Access: $xfinance"
  $xbody += "&lt;p&gt;IT Support Mailbox Access: $xitsupport"
  $xbody += "&lt;p&gt;Hide from GAL: $xhidefromgal"
  
  Send-O365MailMessage -To itsupport@blah.com -Subject "O365 Account Created" -Body "$xbody" -BodyAsHtml -Credential $cred
  </code></pre>
  
  <p>The variable $xpassword may include characters that need escaping, and may not. When the password contains a '&lt;' for example, I get this as one line in my email:</p>
  
  <pre><code>Password: 4*#&lt;(OGyq&lt;="" span=""&gt;
  </code></pre>
  
  <p>How can I cater for this?</p>