How to pause with PowerShell 2.0?

Alright, I'm losing it. PowerShell annoys me. I would like a pause dialogue to appear, and it will not.

PS W:\>>> $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") Exception calling "ReadKey" with "1" argument(s): "The method or operation is not implemented." At line:1 char:23 + $host.UI.RawUI.ReadKey <<<< ("NoEcho") + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException 
+47
powershell
Oct 18 '10 at 20:27
source share
5 answers
 cmd /c pause | out-null 

(This is not a PowerShell method, but it is much wider.)

Save the trees. Use single line.

+44
Aug 23 2018-11-21T00:
source share

I think it's worth demonstrating the choice here for clarity ... then suggest a new option that, in my opinion, provides better utility.

<1> ReadKey (System.Console)

 write-host "Press any key to continue..." [void][System.Console]::ReadKey($true) 
  • Advantage: accepts any key, but correctly excludes the modifier keys Shift, Alt, Ctrl.
  • Disadvantage: does not work in PS-ISE.

<2> ReadKey (RawUI)

 Write-Host "Press any key to continue ..." $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") 
  • Disadvantage: does not work in PS-ISE.
  • Disadvantage: does not exclude modifier keys.

<3> cmd

 cmd /c Pause | Out-Null 
  • Disadvantage: does not work in PS-ISE.
  • Disadvantage: Apparently launches a new shell / window on first use; not noticeable on subsequent use, but still has overhead

<4> Read-Host

 Read-Host -Prompt "Press Enter to continue" 
  • Advantage: Works in PS-ISE.
  • Shortcoming: accepts only the Enter key.

<5> ReadKey composite

This is a composition above 1 using the ISE / kludge workaround extracted from the Adam Tech Blog offer (kindly provided by Nick from the previous comments on this subject page). I made two small improvements for the latter: a test path was added to avoid error if you use Set-StrictMode (right?), And final Write-Host to add a new line after pressing a key to place the prompt in the right place.

 Function Pause ($Message = "Press any key to continue . . . ") { if ((Test-Path variable:psISE) -and $psISE) { $Shell = New-Object -ComObject "WScript.Shell" $Button = $Shell.Popup("Click OK to continue.", 0, "Script Paused", 0) } else { Write-Host -NoNewline $Message [void][System.Console]::ReadKey($true) Write-Host } } 
  • Advantage: accepts any key, but correctly excludes the modifier keys Shift, Alt, Ctrl.
  • Advantage: works in PS-ISE (although only with Enter or mouse)
  • Disadvantage: more than one ship!
+97
Mar 12 '14 at 20:17
source share

I assume you want to read input from the console. If so, use Read-Host .

+33
Oct 18 '10 at 20:30
source share

Solutions like cmd /c pause cause the new shell to start and run in the background. Although this is acceptable in some cases, it is not entirely ideal.

Solutions using Read-Host force the user to press Enter, and are in fact not a "no key."

This solution will provide you with a true โ€œpress any key to continueโ€ interface and will not launch a new interpreter that essentially mimics the original pause command.

 Write-Host "Press any key to continue..." [void][System.Console]::ReadKey($true) 
+9
Mar 12 '14 at 1:59
source share

In addition to Michael Sorens, answer:

<6> ReadKey in the new process

 Start-Process PowerShell {[void][System.Console]::ReadKey($true)} -wait 
  • Advantage: Accepts any key, but correctly excludes Shift, Alt, Ctrl modifier keys.
  • Advantage: Works in PS-ISE.
0
Aug 6 '17 at 13:53 on
source share



All Articles