HomeLinuxMethods to Pause a PowerShell Script

Methods to Pause a PowerShell Script


PowerShell is a scripting device used to carry out Home windows automation and administrative duties. It additionally affords a function of pausing a script. PowerShell scripts are paused so as to add a consumer enter, wait for an additional course of to execute, or decelerate the execution velocity. PowerShell script execution might be paused utilizing particular instructions.

This write-up will stroll us by way of numerous strategies to repair the talked about question.

Methods to Pause a Script in PowerShell?

These are the strategies that may be utilized to pause a script:

Technique 1: Use the “Pause” Cmdlet to Pause a PowerShell Script

The “pause” cmdlet is utilized in PowerShell so as to add a pause in a script. When the “pause” cmdlet will get executed it shows the dialog “Press any key to proceed. . .” when no string enter is given.

Instance

This instance will reveal the working of the “pause” cmdlet:

> write-host “Good day World”
> cmd /c ‘pause’

 
On this said instance:

    • First, we added a string with the assistance “Write-Host” command.
    • After that, we added the “cmd /c ‘pause’” cmdlet to pause the script:

 

As might be seen, the “pause” cmdlet efficiently added a pause within the script.

Technique 2: Use the “Learn-Host” Command to Pause a PowerShell Script

One other technique is a “Learn-Host” to pause a script. This cmdlet prompts for consumer enter and waits till the enter is given.

Instance

Now, pause the script utilizing the “Learn-Host” cmdlet as follows:

> Learn-Host “Press any key to proceed”

 

Technique 3: Use the “Timeout” Command to Pause a PowerShell Script

The “timeout” command pauses the script for a specified variety of seconds. It’s helpful while you need the consumer to attend for the method to execute correctly.

Instance

On this given demonstration, the “timeout” cmdlet will probably be used to pause a script for “5” seconds:

> write-host “Script is paused for five seconds”
> timeout /t 5

 

Technique 4: Use the “Begin-Sleep” to Pause the Script

One other command that’s used mostly is the “Begin-Sleep”. It pauses the script for a specified time frame.

Instance

Now, we are going to make the most of the “Begin-Sleep” cmdlet to pause a script for 5 seconds:

> write-host “Script will stay paused for five seconds”
> Begin-Sleep -Seconds 5

 

That was all about pausing PowerShell script.

Conclusion

A PowerShell script might be paused utilizing numerous strategies. These strategies embrace “Pause”, “Learn-Host”, “Timeout”, or “Begin-Sleep” cmdlets. With a purpose to pause a script, add any of those talked about instructions within the script physique. This text has offered in-depth element to pause a script in PowerShell.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments