PowerShell prompt to continue execution of code

Solution 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.

Solution 2:

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

Solution 3:

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"):

Solution 4:

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.

Solution 5:

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.