Windows

How to pause a PowerShell script

PowerShell is a cross-platform task automation and configuration management framework, consisting of a command-line shell and scripting language. Unlike most shells, which accept and return text, PowerShell is built on top of the . NET Common Language Runtime (CLR), and accepts and returns . NET objects.

You may be amazed to know that to pause a PowerShell script is not as easy and has nothing as equivalent to cmd.exe’s “Pause” command. I will be discussing various ways to implement the pause function in a PowerShell script.

Note: You may have to set the Execution Policy for PowerShell in order to be able to run the PowerShell script. For other articles relating to PowerShell, see the following link.
– Ensure to run the PowerShell as an Administrator.

Just enter the PowerShell script into your existing script in order to have it paused.

Function Pause($M="Press any key to continue . . . "){If($psISE){$S=New-Object -ComObject "WScript.Shell";$B=$S.Popup("Click OK to continue.",0,"Script Paused",0);Return};Write-Host -NoNewline $M;$I=16,17,18,20,91,92,93,144,145,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183;While($K.VirtualKeyCode -Eq $Null -Or $I -Contains $K.VirtualKeyCode){$K=$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")};Write-Host}

What you could also try out is using the command below. The "/c" Carries out the command specified by string and then terminates | out-null pipes the output to out-null, where you'll never see it If you want to see the "Press any key to continue..." message, remove the pipe but does not work in PowerShell ISE as it gets stalk.

cmd /c pause | out-null

I hope you found this blog post helpful. If you have any questions, please let me know in the comment session.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x