$inputYN = Read-Host "(defaults to y in 10 sec) [y/n] ";
If user doesn't provide any input in 10 second then default $inputYN
should go to "Yes" and move forward to next task.
$inputYN = Read-Host "(defaults to y in 10 sec) [y/n] ";
If user doesn't provide any input in 10 second then default $inputYN
should go to "Yes" and move forward to next task.
Your question intrigued me. I'm not sure how well this will work, but I tested it a couple of times and got the correct keys back. Doesn't work too well if you try to enter capital letters. There has to be a better way than this, though. You can always check with other forums like PowerShell.com or PowerShell.org to get a different crowd.
function Get-Answer {
$counter = 0
while($counter++ -lt 100){
if($Host.UI.RawUI.KeyAvailable){
#You could check to see if it was the "Shift Key or another"
$key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp")
break
}
else{
Start-Sleep -Milliseconds 100
$key = $false
}
}
return ($key)
}
Write-Host 'Enter y/n'
$answers = Get-Answer
if($answers -eq $false -or ($answers.Character -eq 'y')){
"Yes is default"
}
elseif($answers.Character -eq 'n'){
"NO!!!!"
}
else{
"Invalid Key $($answers.Character): `"YES`" is being used now."
}