Last active
December 19, 2023 23:21
-
-
Save csev1755/0fb9002fbb9906a3701c21a608fb51ee to your computer and use it in GitHub Desktop.
Let user confirm input in PowerShell script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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