In PowerShell 5.0, a new .NET type was introduced that can help you escape text information so it can be displayed correctly:
PS> [System.Management.Automation.Language.CodeGeneration] | Get-Member -Static TypeName: System.Management.Automation.Language.CodeGeneration Name MemberType Definition ---- ---------- ---------- EscapeBlockCommentContent Method static string EscapeBlockCommentContent(string value) EscapeFormatStringContent Method static string EscapeFormatStringContent(string value) EscapeSingleQuotedStringContent Method static string EscapeSingleQuotedStringContent(string value) EscapeVariableName Method static string EscapeVariableName(string value) ReferenceEquals Method static bool ReferenceEquals(System.Object objA, System.Object objB)
For example, if you want to display single quotes inside a single quoted string, here is how the text would need to look like:
PS> [System.Management.Automation.Language.CodeGeneration]::EscapeSingleQuotedStringContent("Contacting 'Bill'") Contacting ''Bill''