hellen_dorandt89
hellen_dorandt89

Reputation: 457

Flatten an array variable into a single string, while retaining clean formatting

I have a variable called $Properties, it consists of 3 arrays (All seperated into three lines):

NTER 1, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"
NTER 2, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"
NTER 3, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"

I wanto flatten the $Properties arrays into a single string variable, that looks exactly as above example but with no arrays, so something like this:

NTER 1, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"
NTER 2, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"
NTER 3, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"

I've tried $Var = $Properties -join '' but this takes all three lines and creates a single line:

NTER 1, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"NTER 2, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"NTER 3, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"NTER 4, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"LA2 1, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"LA2 2, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"LA2 3, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"LA2 4, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"

I've also tried $Var = $Properties -join "$null" , still the same issue. Any ideas would be wonderfull. Thanks!

Am on Powershell 7.3 (Preview)

Upvotes: 0

Views: 68

Answers (1)

charmian
charmian

Reputation: 481

If you want to join the strings array with a line break, you can use

  • "`r`n"(CRLF) for Windows style, or
  • "`n"(LF) for Linux style.

Use $Var = $Properties -join "`r`n" in the code

$Properties = @(
  'NTER 1, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"'
  'NTER 2, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"'
  'NTER 3, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"'
)
$Var = $Properties -join "`r`n"

It will generate

NTER 1, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"
NTER 2, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"
NTER 3, ShortD = "Start > Then Dump", LongD = "Procces is started, then all errors are dumped to a log file"

Upvotes: 1

Related Questions