Powershell script indentation for long strings
You can't escape away the tabs or spaces you inserted into the string; they're inside the quotes and part of the string now (and so are the line breaks for that matter).
You have a few options:
- Don't break the string at all; let it ride. Obviously not what you want to do, but it works.
- Use @Matt's suggestion and concatenate the strings. As long as the quotes begin after the indentation, you'll be fine.
Don't indent the next lines. This assumes you do want the line breaks as part of the string. It can look messy, but it will be a) readable without scrolling and b) functional. It looks something like this:
if ($fakeCondition) { if ($fauxCondition) { $longString = "Hello this string is too long to be just one big line so we'll break it up into some smaller lines for the sake of readability." # Code that uses the string Write-Host $longString } }
Other stuff: use an array with one element for each line and then join it, or use .NET's
StringBuilder
, but those are overkill to solve what is essentially a formatting annoyance for you.Here strings (using
@"
to begin a string and"@
to end it) will have the same problem as option 3 (the lines cannot be indented or the tabs/spaces will be embedded in the string).
My View
When I run into this issue of long strings polluting the code, I usually start to rethink embedding the strings, or where I'm embedding them.
I might break this functionality into a function, then accept the string as a parameter (pushing the problem off to a different part of the code, but it can be helpful).
Sometimes I'll put it into a here document or long string at the top of the script and then use the variable later on.
Sometimes it means saving these strings in a separate file and reading the contents at run time.
All depends on the situation.
I'd like to share some options in addition to other answers.
Joining array of substrings (explicit)
Unary -join
operator
$singleLineText = -join @(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
"tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
"veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
"commodo consequat."
)
Binary -join
operator
$singleLineText = @(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod"
"tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim"
"veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea"
"commodo consequat."
) -join ' '
Pros:
- No plus signs (+) or commas (,) needed.
- Easy switch to binary
-join "`r`n"
operator for multiline string. - Free to use desired indentations.
Cons:
- Text manipulations can be tiresome.
Joining array of substrings (implicit) | avoid
Appending to an empty string.
$singleLineText = '' + @(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod"
"tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,"
"quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo"
"consequat."
)
Piping to script block and using $input
- an automatic variable.
$singleLineText = @(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod"
"tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,"
"quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo"
"consequat."
) | & { "$input" }
Whenever an array is coerced into a string, [System.String]::Join(separator, array)
method is applied implicitly. Where the separator is " " (space) by default, and can be overwritten by setting $OFS
- the Ouptut Field Sperator special variable.
Pros:
- Suited for joining pipe output.
Cons:
- Lack of clarity (for several reasons), thus should be avoided whenever possible.
Here-string
$singleLineText = @"
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
"@ -replace "`r" -replace "`n", ' '
Pros:
- Good for drop-in big sheets of arbitrary preformatted text (like source code).
- Preserves source indentations.
- No need to escape quotes.
Cons:
- Not friendly to script formatting.
- Can be hard to keep track of trailing white-spaces.
The addition assignment operator (+=
)
$s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do ";
$s += "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad ";
$s += "minim veniam, quis nostrud exercitation ullamco laboris nisi ut ";
$s += "aliquip ex ea commodo consequat.";
Pros:
- Most obvious, universal and well-known syntax (outside of PowerShell).
- Mostly programming language agnostic *.
* Variable naming conventions may be violated in some programming languages.
Cons:
- Text manipulations can be tiresome.