Skip to content

Instantly share code, notes, and snippets.

@bender-the-greatest
Created December 27, 2016 15:40
Show Gist options
  • Save bender-the-greatest/2d5bff211684727ffa226937d753cfc8 to your computer and use it in GitHub Desktop.
Save bender-the-greatest/2d5bff211684727ffa226937d753cfc8 to your computer and use it in GitHub Desktop.
Powershell function to convert a multiline string to a single line string with line break characters
# Powershell function to convert a multiline string to a single line string with line break characters
#
# Example usage:
# # Replace line breaks with \r\n (Windows-style line endings)
# cat 'nginx.cer' | Convert-MultilineToSingleLine
# # Replace line breaks with \n (Unix/Linux-style line endings)
# cat 'nginx.cer' | Convert-MultilineToSingleLine -n
function Convert-MultilineToSingleLine {
Param(
[Parameter(ValueFromPipeline=$True,Mandatory=$True)]
[string]$multilineString,
[switch]$nixEnd = $False
)
begin {
$fullPipeline = ""
$linebreak = "\r\n"
if ($nixEnd) {
$linebreak = "\n"
}
}
process { $fullPipeline += $multilineString + $linebreak }
end { $fullPipeline }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment