I needed a "pause" command for some of my PowerShell scripts, so this is what I ended up with. Enjoy.
param( [string]$message = "Press any key to continue..." ) Write-Host -NoNewLine $Message $Host.UI.RawUI.FlushInputBuffer() do { $key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown").Character } while ([int]$key -eq 0) Write-Host ""
You might want to use:
$dontcare = $Host.UI.ReadLine()
with the caveat that they have to press Enter, so you're not spinning the CPU polling (and you get to have those nostalgic "Press Enter to Continue" prompts) :)
Posted by: Paul Betts | August 22, 2009 at 15:50
ReadKey doesn't spin the CPU.
Posted by: Brad Wilson | August 22, 2009 at 16:16
@Brad - Hmmm, so it doesn't; why add the do loop then?
Posted by: Paul Betts | August 22, 2009 at 16:50
ReadKey returns even for control and dead keys. The check for character == 0 prevents the pause from accepting the user pressing "ctrl" or "shift", for example.
Posted by: Brad Wilson | August 22, 2009 at 17:09
It also needed a couple of commands will be found in his blog thanks
Posted by: viagra online | May 24, 2010 at 15:35