Waiting to press “Enter” in a PowerShell script

I. Introduction

Although PowerShell is very useful for automating a maximum number of actions and facilitating system administration, there are sometimes actions that need to be carried out manually. Let's say we have 10 actions to perform, and the 5th action can't be performed by a script. How can we make our script wait while we perform the operation ourselves, before it continues with the 6th step?

We could use the Start-Sleep cmdlet to define a fixed pause of X seconds or minutes, but we need to be sure of our timing... Or we could simply ask the PowerShell script to wait for the ENTER key on the keyboard to be pressed in the script execution console: this allows us to give the "go" manually, when we're ready for the rest of the execution.

II. Wait to press "Enter

In PowerShell, there's a cmdlet that's used to retrieve user input from the console, called"Read-Host", as we saw in the previous chapter. When we make an entry, to validate it, we press the "Enter" key! See what I mean?

In fact, it's quite simple: at the point where you want to pause and wait for the "Enter" key to be pressed, simply include a line like this:

Read-Host "Press ENTER to continue..."

When the script is executed, the following message will appear in the console:

Press ENTER to continue...

Simply press the magic key when you're ready, and the script will continue to run... This is how to pause and resume manually. This method can be used in any of your scripts.