In this write-up, we have demonstrated the purpose, working, and usage of PowerShell pause methods.
Let’s dig into them one by one.
Method 1: Start-Sleep
The Start-Sleep suspends the execution of the code for a specific time period. The user can also specify the time period in milliseconds or seconds. The following code refers to a PowerShell script where the script execution will pause for 5 seconds before executing the other part of the script:
Start-Sleep -Seconds 5
Write-Host "Script ended!"
In the above code:
- The first line simply displays a message using the Write-Verbose method.
- In the second line, the Start-Sleep method is used by assigning a specific time period -Second 5.
- In the end, the script is executed after pausing for the given 5 seconds.
Now, run the script by copying and pasting the script path into the PowerShell terminal.
It is observed from the output that the script execution was paused for some time until the specified time limit was reached.
Moreover, you can specify the time in milliseconds, as we did in the below-mentioned command to pause the script for 1000 milliseconds:
The above-written line will pause the script execution for 1000 milliseconds.
Output
The above display pauses the script for 1000 milliseconds in PowerShell.
Method 2: Pause Command
The pause command stops the execution of the script in PowerShell for an undefined time (until the enter key is pressed). You need to write the pause keyword wherever the user wants to use this command. The example code is provided below:
Code
pause
Write-Output "Starting entering process"
The above script uses the pause command to stop the execution in PowerShell.
Output
The display shows that the pause method interrupts the script until the user presses Enter to continue in PowerShell.
Method 3: Thread Sleep
One of the methods to halt the execution of scripts in PowerShell is the Sleep() method. The Sleep() method uses the Thread class of the System.Threading method. The waiting time can be measured in seconds or milliseconds. Let’s practice this using the following code:
Code
In the above code, users manually give a 2300 millisecond value inside the Sleep() method.
Output
The script will pause for 2300 milliseconds before proceeding to further execution.
Method 4: Console.ReadKey()
The Console.ReadKey() method can be used to pause the script’s execution until the user presses any key. Its working procedure is similar to the pause method. The pause method accepts the enter key to continue the execution while the Console.ReadKey() method accepts any key to continue the execution. We have practiced this method in the below-stated example script:
Code
Write-Output "Halt the execution till the key is pressed"
[Console]::ReadKey()
Write-Output "Resume the Execution Script"
The code comprises two messages before the Readkey() method and one message after it to check the interruption.
Output
The output shows that the ReadKey() method requires any key. The execution is halted until the user provides any input.
Method 5: Read-Host
In PowerShell, the Read-Host command is used to take input from users. The interpreter does not move to the next line until the user provides any input or hits an enter key. The following script makes use of the Read-Host in cmdlet:
$name = Read-Host "Enter Name to print"
Write-Output "Name = $name"
In the above script, the $name variable is used to store the value provided by the user input. After that, the Write-Output cmdlet is used to display the name stored in the $name variable.
Output
The output shows that Read-Host requires input from the users and produces the outcome for further processing.
Method 6: Timeout
The purpose of the timeout command is to stop the execution of a script for a specific time period. The timeout command is used to pause the script in PowerShell. Using the command, the user can specify the waiting time with the \t option.
In the above code, the user manual assigns the 5 seconds with the help of the timeout command in PowerShell. It can be used in multiple places of the code to pause the script depending on the user’s needs.
Output
After executing the command, the timer starts at 5 seconds, and the script will pause until the timer reaches 0 seconds.
Conclusion
PowerShell supports a list of methods and cmdlets that can be referred to as PowerShell pause methods. These methods include Start-Sleep, Pause command, Read-Host, Thread Sleep, Timeout, Console.ReadKey(), and so on. You have learned all these pause methods and their practical execution. Each method comprises a distinct set of functionalities. For instance, the Start-Sleep will halt the execution for a limited time, whereas the Read-Host will pause the script until the user provides any input or presses any key.