How To Pause and Resume Powershell and Cmd Scripts In Windows With Examples?
Powershell is as its name suggest powerful language. I have very good features and libraries. In this tutorial we will look how to pause Powershell in different ways and different behaviors.
Pause And Wait Input
We may want to pause the current shell and wait an input from the user. This is very useful if we need more specific input from the user like selecting a menu or an option. In this example we will read input from users interactive shell which will pause the execution of the shell up to an input is entered by the user.
$User = Read-Host 'Input the user name'
Write-Host "I got it"
Pause For Specified Time Period
We have another options where we can specify the time as seconds to wait. This will actually sleep the current running interactive process and wait for the given time. After the sleep period end the script will resume executing from point where it left. In this example we will write
"I will sleep" in the console and then sleep for 5 seconds after the sleep we will write
"I woke up".
Write-Host "I will sleep"
Start-Sleep -s 5
Write-Host "I woke up"
Sleep In MS-DOS
MS-DOS or cmd also supports for sleeping functionality. This is a bit different from previous examples. This code is more elegant and simple. We will implement code in a single line or one liner. we will issue
pause command and print the output to
null. We can use this line as in a batch file.
$ cmd /c pause | out-null