powershell – Press any key to continue

powershell – Press any key to continue

Here is what I use.

Write-Host -NoNewLine Press any key to continue...;
$null = $Host.UI.RawUI.ReadKey(NoEcho,IncludeKeyDown);

Ive created a little Powershell function to emulate MSDOS pause. This handles whether running Powershell ISE or non ISE. (ReadKey does not work in powershell ISE). When running Powershell ISE, this function opens a Windows MessageBox. This can sometimes be confusing, because the MessageBox does not always come to the forefront. Anyway, here it goes:

Usage:
pause Press any key to continue

Function definition:

Function pause ($message)
{
    # Check if running Powershell ISE
    if ($psISE)
    {
        Add-Type -AssemblyName System.Windows.Forms
        [System.Windows.Forms.MessageBox]::Show($message)
    }
    else
    {
        Write-Host $message -ForegroundColor Yellow
        $x = $host.ui.RawUI.ReadKey(NoEcho,IncludeKeyDown)
    }
}

powershell – Press any key to continue

Check out the ReadKey() method on the System.Console .NET class. I think that will do what youre looking for.

http://msdn.microsoft.com/en-us/library/system.console.readkey(v=vs.110).aspx

Example:

Write-Host -Object (The key that was pressed was: {0} -f [System.Console]::ReadKey().Key.ToString());

Leave a Reply

Your email address will not be published.