Skip to content

Instantly share code, notes, and snippets.

@csev1755
Last active December 19, 2023 23:21
Show Gist options
  • Save csev1755/0fb9002fbb9906a3701c21a608fb51ee to your computer and use it in GitHub Desktop.
Save csev1755/0fb9002fbb9906a3701c21a608fb51ee to your computer and use it in GitHub Desktop.
Let user confirm input in PowerShell script
function Confirm-UserInput {
param ($prompt)
# Get input
$answer = Read-Host $prompt
while ($True) {
# Prompt for confirmation
$choice = Read-Host "Entered: $answer`nContinue? (y/n)"
# Return if "y/Y"
if ($choice.ToLower() -eq "y") {return $answer}
# Get new input if "n/N" and restart loop
elseif ($choice.ToLower() -eq "n") {
$answer = Read-Host $prompt
continue
}
# Return to top of loop if confirmaation input is invalid
else {continue}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment