Scenario / Questions

I have a script I am using to automate WSUS processes, and the last stage of it goes on to remove all old/unnecessary files/objects.

I would like to prompt ‘Press ‘Enter’ to continue with removal or any other key to stop’ before the cleanup stage to give people the option to not run it.

The code I currently have at the end of the script is here:

Get-WsusServer -PortNumber 8530 | Get-WsusUpdate -Classification All -Approval Unapproved -Status FailedOrNeeded | Approve-WsusUpdate -Action Install -Target $ComputerTarget -Verbose

Write-Host "Updates have been approved!"
Write-Host "Preparing to clean WSUS Server of obsolete computers, updates, and content files."

#Part2 - WSUS Server Cleanup

##Run Cleanup Command
Get-WsusServer $WSUS_Server -PortNumber $PortNumber | Invoke-WsusServerCleanup -CleanupObsoleteComputers -CleanupObsoleteUpdates -CleanupUnneededContentFiles

Just prior to #Part2 I would like to have the prompt ‘Press enter to continue or any other key to abort’

Is there a simple way to do this?

Everything I’ve seen appears to involve nesting the entire script inside of a code block which I’d rather not do. =/

Find below all possible solutions or suggestions for the above questions..

Suggestion: 1

Another simple solution would be to use:

Read-Host -Prompt "Press any key to continue or CTRL+C to quit" 

I believe this is a better solution to the currently accepted answer because the requirement of hitting enter on the keyboard. I don’t believe hitting enter will accept the UI prompt unless that UI element is in focus.

Suggestion: 2

Just add -confirm to your Invoke-WsusServerCleanup command. It’s built in.

Suggestion: 3

See Pausing a Script Until the User Presses a Key

The relevant script lines are:

Write-Host "Press enter to continue and CTRL-C to exit ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

You can add a check for enter and wrap this is a loop if you really really want just one key to continue. You can also add an else to exit the script but I’d recommend just reminding the user that ctrl-c will exit. Why code for something that’s built in.

Suggestion: 4

At whatever step you’d like PowerShell to hold at, write Pause in your code. PowerShell will sit at “Press Enter to continue…:” until you hit Enter or close the shell/ISE.

Suggestion: 5

You Can use write-warning option. quite sleek:

Write-Warning "This is only a test warning." -WarningAction Inquire
WARNING: This is only a test warning.
Continue with this operation?
 [Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help (default is "Y"):

Suggestion: 6

You could insert a Read-Host cmdlet and then process the input value the way you want.

$userInput = Read-Host 

Suggestion: 7

Building this answer just as a follow-up add-on based on the OPs comment about how to validate input, as an addition to the other good answers.. It was too long to put into a comment at the right place.

Validating input can be done in several ways. By way of personal preference I like using the switch statement for input validation, as I generally find it easier to read and debug than a bunch of if elses and more versatile than a do while.

Similarly I prefer using functions over loops for failing the validation, as I find the code cleaner and more reusable. Functions also have the built in possibility of parameter validation where that seems like the best way of validating the content of a variable.

So just as an example, here is a simple function which calls itself to restate the question when the input is not as expected.

function Get-SomeInput {
    $input = read-host "Please write yes or no and press Enter"

    switch ($input) `
        'yes' {
            write-host 'You wrote yes'

        'no' {
            write-host 'You wrote no'

        default {
            write-host 'You may only answer yes or no, please try again.'