I need to echo a series of elements of an array in PowerShell, but provide various delimiters between the elements, so I'm using;
Add-Content -Path $tempInputDir\testoutput.log -value ($($fields[0]) + " "+ $($fields[1]) + " " + $($fields[2]) + " " + $($fields[3]) + " "+ $($fields[15]) + " " + $($fields[17])) }
I need to be able to add tabs and space characters, as you can see from the code above I've just done this by physically adding tabs and spaces in between double quotes, but I'm sure this will cause problems down the line.
What's the correct way to echo these characters to a file? I read somewhere that "'t" could be used, but that doesn't seem to work?
You can use `t
for a tab character in a double quoted string. You can also simplify the above to:
"$($fields[0]) $($fields[1]) $($fields[2]) $($fields[3]) $($fields[15]) $($fields[17])" | Add-Content $tempInputDir\testoutput.log
To join the nominated fields together with tabs:
[string]::join("`t", (0..3,15,17 | % {$fields[$_]}))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With