How to Pause a PowerShell Script

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

PowerShell is a powerful automation tool for Windows. It allows you to perform tasks quickly, and easily in batch scripts. However, it can sometimes be difficult to stop the script from executing due to PowerShell’s execution policy which determines how your script behaves when it encounters errors or other anomalies that prevent it from continuing its normal workflow. This tutorial will show how pause your PowerShell scripts with ease!.

The “powershell pause until keypress” is a command that allows you to pause a PowerShell script. This command will be used in the following example:

How to Pause a PowerShell Script

There are a variety of reasons to halt the execution of a script. Before going on, a script may need to wait for user input, slow down execution pace, or wait for another process to finish. There are several ways to suspend a script with Powershell, as with other activities. The manner of stopping you pick will have pros and limitations depending on what you’re attempting to achieve. PowerShell’s new cross-platform capability adds another wrinkle to the available choices. Because PowerShell is often used to “glue” many technologies together, the Linux command-line offers its own options for halting execution that may be included into PowerShell.

PowerShell Pause Methods

What are the various methods for pausing script execution? The ability to pause will be broken down into native and non-native commands in this article. We use the term “native” to refer to PowerShell or.NET built-in functions or methods. Non-Native Procedures are applications that are only available on Windows and Linux that may be used in combination with PowerShell.

Native Procedures

  • Start-Sleep
  • [System.Threading.Thread]::Sleep()
  • Read-Host
  • [Console]::ReadKey()
  • $host.UI.RawUI.ReadKey(“NoEcho,IncludeKeyDown”)

Non-Native Procedures

Windows

Linux

As you can see, there are a variety of solutions available, each with its own set of advantages and disadvantages. Which one you choose will have an impact on how the execution is halted and what additional repercussions that pause may have.

Native Procedures

To start with, we will begin outlining the Native Procedures as they are most used in scripts.

Start-Sleep

Start-Sleep is by far the most regularly used Command to pause. -Seconds and -Milliseconds are two easy inputs for this command. Seconds may be used to create a system. The value of a double number is taken into account, while milliseconds are taken into account solely by the system. Values of type Int32. The duration of the pause may be precisely controlled by mixing the two. Sleep is also an alias for Start-Sleep.

Start-Sleep -Milliseconds 300 -Seconds 2

You couldn’t put a fractional seconds number like 2.3, which is the same amount of time we’re stopping for in the preceding code, before PowerShell 6.2.0. Fractional numbers are now supported in version 6.2.0, so instead of putting down the seconds and milliseconds separately, you can now say: -Seconds 2.3.

It’s vital to remember that you may use Ctrl-C to exit a Start-Sleep function. This isn’t the case with all approaches!

Sleep on a thread

The [System.Threading.Thread]::Sleep().NET function is a less widely used means of halting execution. The current thread may be paused for a specific period of time by handing in a System.Int32 or a TimeSpan value. For example, to pause for the same period of time as our Start-Sleep example, 2.3 seconds, we may write:

[System.Threading.Thread]::Sleep(2300)

To use a timespan construct with the Sleep on a thread method, you can do the following:

[System.Threading.Thread]::Sleep (New(0, 0, 0, 2, 300)) ([TimeSpan]::New(0, 0, 0, 2, 300)) ([TimeSpan]::New(0, 0, 0,

You may use [System.Threading.Timeout]::InfiniteTimeSpan to suspend a thread forever, but bear in mind that this may freeze up your process.

Unlike Start-Sleep, the Sleep on a thread method does not allow you to break out of the sleep routine using Ctrl-C. Depending on your application, this may be preferable.

Also important to know about Sleep on a thread is that it is not an exact time guarantee. All Sleep on a thread has a guarantee that it will wait at least that many seconds. Since Sleep on a thread counts coincide with clock ticks, then other programs may interrupt this pause function and force the wait to take longer.

While Sleep on a thread has its uses, it is better to use one of the other built-in methods because of these issues.

Read-Host

The techniques listed above show how to suspend execution for a certain length of time. Another technique to halt execution is to ask the user for feedback. Although this technique requires a human activity, it may be highly beneficial to need confirmation before proceeding and perhaps incorporate extra data.

One of the most frequent methods to ask for user input is to utilize Read-Host. The resultant item may be kept for further use in the code, or used to suspend execution as needed, by requesting for human input.

“Press any key to continue,” Read-Host -Prompt

To move the execution along, use the enter or return key once the text has been typed. This will output the same text typed into the terminal without storing the input to a variable.

To forward the execution, press Enter.To forward the execution, press Enter.

Simply assign the Read-Host input to a variable if you wish to preserve the resultant text entered for later use.

$Input = “Press any key to continue,” Read-Host -Prompt

You may wish to read in the input as a secure string at times. Thankfully, Read-Host simplifies the process.

$SecureInput = Read-Host -Prompt “Enter your password” -AsSecureString $SecureInput = Read-Host -Prompt “Enter your password” -AsSecureString $SecureInput

You may exit the Read-Host prompt by pressing Ctrl-C.

The ReadKey method on the console

You may quickly read in key commands and modifiers while halting the application execution using the [Console]::ReadKey() function. The key commands are repeated back to the screen as a System by default. ConsoleKeyInfo is a kind of object. This contains the values for Key, KeyChar, and Modifiers, which may be quite handy when looking for certain key combinations.

ReadKey Command on the ConsoleReadKey Command on the Console

Before continuing on, you may wish to verify for a certain key input. A Do-While loop, such as the one shown below, may easily do this.

Do $Key = [Console]::ReadKey($True) $Key = [Console]::ReadKey($True) $Key = [Console]::ReadKey( While ($Key.Key -NE [ConsoleKey]::Escape) Write-Host $Key.Key

Key to the Do-While LoopKey to the Do-While Loop

Ctrl-C will not break out of the input here since we’re seeking for a certain key combination.

RawUI ReadKey Method Host

Finally, we have the $host.UI.RawUI.ReadKey() method. This is similar to the The ReadKey method on the console, but this method allows for passing in additional options into the ReadKey method to control behavior and output further.

RawUI.ReadKeyRawUI.ReadKey

There are many ReadKeyOptions that may be provided in to improve the RawUI ReadKey method’s functionality.

  • AllowCtrlC
  • IncludeKeyDown
  • IncludeKeyUp
  • NoEcho

This may be utilized in the following way: key down events are included, but the text is not echoed out.

$host.UI.RawUI.ReadKey(‘NoEcho,IncludeKeyDown’)

Except if the AllowCtrlC option is enabled, this technique will allow the usage of Ctrl-C to exit the command.

Non-Native Procedures

In this section, we explore a few non-Native Procedures of pausing script execution. These methods are intended for utilizing non-PowerShell console-specific utilities that can be integrated into PowerShell scripts depending on your needs.

Windows

Pause

The Command to pause is straightforward and will result in a display. To continue, press any key… and it will stay that way until a key is touched to restart execution.

Command to pauseCommand to pause

You may wonder how to use this in a script, simply paste the cmd /c ‘pause’ command into your script just like any other command to utilise the native capability.

Cmd/C Command to pauseCmd/C Command to pause

Timeout

The Command for a timeout, which is often seen in Windows batch files, enables you to pause for a set amount of seconds while also ignoring any key commands. Even if the timeout period hasn’t expired, a user input will usually restart execution. It is simple to use by just entering the amount of seconds.

# Pause for 2 seconds timeout /t 2 # Pause for 5 seconds timeout /t 5 /nobreak # Pause for 5 seconds but don’t allow keystroke breaks timeout /t 5 /nobreak # Timeout /t -1 pause forever till a key is hit

Command for a timeoutCommand for a timeout

Just as with the Command to pause, insert the Command for a timeout within the script and in the example below, we hit the enter key to continue.

Command for a timeout ExecutionCommand for a timeout Execution

Linux

Sleep

Like the Command to pause in Windows, the Command to Sleep in Linux allows for setting an arbitrary value for sleep. Unlike the Command to pause, the Command to Sleep has a few more options that allow for flexibility in sleep time.

Command to SleepCommand to Sleep

The Command to Sleep also optionally has suffixes that can allow for longer periods of time to be defined. Examples:

  • sleep 1m – Take a one-minute nap.
  • sleep 2h – Take a two-hour nap.
  • 1d sleep – 1 day’s worth of sleep

Read -P

Finally, Linux may pause by utilizing the read program to wait for the enter key to be pressed in order to resume operation. If the enter key is not pressed within the set period, the program will continue to run.

read -p ‘To continue, press enter’ -t 5

Pause the application by reading it.Pause the application by reading it.

Conclusion

As you can see, there are many ways to pause execution within a script or on the command line. Depending on your needs, it could be as simple as a Start-Command to Sleep or a more complex input-driven process. Combined with native commands, PowerShell pause allows for flexibility in nearly any environment and maximum script utility.

The “add wait time in powershell script” is a method that can be used to pause a PowerShell Script. This will allow the user to add a delay before the script continues on with its work.

Frequently Asked Questions

How do you pause a PowerShell script?

A: You can pause a PowerShell script by pressing the Windows+Z key combination, which will stop all execution of your current command.

Is there a pause command in PowerShell?

A: Yes. There is a pause command in PowerShell, but it doesnt stop the script from running; instead, it pauses and then continues once the user presses pause again or resumes their script by pressing enter on a new line of code.

How do I pause a PowerShell script for 10 seconds?

A: To pause a PowerShell script for 10 seconds, you must type pause on the command line.

Related Tags

  • how to pause powershell script after execution
  • pause powershell script before exit
  • start-sleep powershell
  • powershell pause screen output
  • powershell pause for 15 seconds