I’m sure this is quite a simple problem, but I’ve searched around and haven’t found a similar question. Here’s my code:
function FormallyAsk([string] $question)
{
$line = $question.Split([System.Environment]::NewLine)
$line
}
$answer = FormallyAsk(@"
Are you sure you want to quit?
[ Yes ] [ No ]
"@)
Write-Output([string]::Join("<->", $answer))
When I run this, my output looks like this:
Are you sure you want to quit?<-><-><-><->[ Yes ] [ No ]
I don’t understand why I’m getting double the number of new line splits I would expect. I took a look at the binary for the script I’m working on and it has normal Windows line endings:
Does anyone know why my output looks so unexpected? It’s possible I am misunderstanding how it should work, but I’m just going by my experience with split methods in other languages.
If this isn’t just an example and your actual issue is something else I’d recommend using another method asking for user input. There is a dotNet method called PromptForChoice you could leverage to get a users choice.
I ran your code and got only two “<->” separators. Just to verify that your assumptions are correct, place this line between line 11 “@) and line 13 Write-Output(\[string\]::Join("<->", $answer)):