I have a function which inserts a Y or N menu when called within my Powershell scripts. It uses a while loop to validate that either a Y or N value is entered. Everything works fine, however a new line is created each time an error is made. I could use cls and redisplay everything, but this is not the most ideal solution. Instead, I would like to find a way to redisplay the read-host prompt on the same line while clearing any previously entered answer. Here is my existing code:
# Begin function to display yes or no menu
function ynmenu {
$global:ans = $null
Write-Host -ForegroundColor Cyan "`n Y. [Yes]"
Write-Host -ForegroundColor Cyan "N. [No]`n"
While ($ans -ne "y" -and $ans -ne "n"){
$global:ans = Read-Host "Please select Y or N"
}
}
# End function ynmenu
I have a few other dynamically populated menus which leverage this methodology. Finding a solution to this would resolve the issue with those as well.