Skip to content

Instantly share code, notes, and snippets.

@wightsci
Last active October 23, 2019 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wightsci/dd349c42f43ccef488de1edf4008ced0 to your computer and use it in GitHub Desktop.
Save wightsci/dd349c42f43ccef488de1edf4008ced0 to your computer and use it in GitHub Desktop.
Function Format-XMLText {
Param(
[Parameter(ValueFromPipeline=$true,Mandatory=$true)]
[xml[]]
$xmlText
)
Process {
# Use a StringWriter, an XMLWriter and an XMLWriterSettings to format XML
$stringWriter = New-Object System.IO.StringWriter
$stringWriterSettings = New-Object System.Xml.XmlWriterSettings
# Turn on indentation
$stringWriterSettings.Indent = $true
# Turn off XML declaration
$stringWriterSettings.OmitXmlDeclaration = $true
# Create the XMLWriter from the StringWriter
$xmlWriter = [System.Xml.XmlWriter]::Create($stringWriter,$stringWriterSettings)
# Write the XML using the XMLWriter
$xmlText.WriteContentTo($xmlWriter)
# Don't forget to flush!
$xmlWriter.Flush()
$stringWriter.Flush()
# Output the text
$stringWriter.ToString()
# This works in a remote session, when [Console]::Out doesn't
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment