Choice command equivalent for Powershell allowing over 254 characters

68 Views Asked by At

Is there a choice command equivalent for Powershell that can allow over the 254 character limit? As the choice command functions, it only allows specified characters. Such as in the example below, only the Y or N characters can be typed.

choice /C YN /M "Y or N?"
if ($LASTEXITCODE -eq 2) {
    Write-Output "You said N"
}
elseif ($LASTEXITCODE -eq 1) {
    Write-Output "You said Y"
}
1

There are 1 best solutions below

0
mklement0 On

To spell it out: the /m parameter of choice.exe, which specifies the prompt message, is limited to 254 characters, and choice.exe fails with longer arguments.

To overcome this limit, you don't need an alternative to choice.exe, however:

  • Use Write-Host-NoNewLine to print your 254+-character message, followed by a space.
  • Then call choice.exe without /m:

For instance:

Write-Host -NoNewLine ('x' * 254 + '! ')
choice /c yn