Share via


Why do I get an exception calling 'ReadKey'?

Question

Friday, February 8, 2013 6:13 PM

Hello,

I run the following code in my script.  What displays is 'Press any key to continue' which is the intent, but then the following error occurs without a key being pressed and the script proceeds.  I dont understand the error. Any insight as to why I see this error?

write-Host "Press any key to continue.."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Exception calling "ReadKey" with "1" argument(s): "The method or operation is not 
implemented."
At C:\adtest.ps1:247 char:1
+ $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NotImplementedException

Thanks for your help! SdeDot

All replies (10)

Saturday, February 9, 2013 9:00 AM ✅Answered | 6 votes

You can't using this method in Powershell ISE host by design.

http://connect.microsoft.com/PowerShell/feedback/details/713218/host-ui-rawui-readkey-not-implemented-in-version-3-ctp2


Friday, February 8, 2013 6:37 PM

Your code works for me with PS 3

http://blogs.msdn.com/b/powershell/archive/2007/02/25/pause.aspx

has some similar code from Feb 2007.


Friday, February 8, 2013 6:55 PM

What version of PowerShell are you using? Just in case you might be running V1, run the following command:

$host.version

Boe Prox
Blog | PoshWSUS | PoshPAIG | PoshChat


Friday, February 8, 2013 7:25 PM

It occurs to me that the the $host object might be different depending on which PowerShell host is in place.

I'm using the powershell.exe host.

Here is an alternative:

http://adamstech.wordpress.com/2011/05/12/how-to-properly-pause-a-powershell-script/


Friday, February 8, 2013 7:38 PM

Thanks Larry and Boe.

Boe: This is running on PS 3.0 on a Server 2012 box.

Thanks for your help! SdeDot


Monday, February 1, 2016 8:47 PM

Dead link.  Any idea what it said?


Monday, February 1, 2016 8:51 PM

Dead link.  Any idea what it said?

EDIT: Actually this is still the case. See Larry's link.


Monday, February 1, 2016 9:02 PM | 1 vote

Try

https://web.archive.org/web/20141228010957/https://connect.microsoft.com/PowerShell/feedback/details/713218/host-ui-rawui-readkey-not-implemented-in-version-3-ctp2


Wednesday, February 1, 2017 6:57 PM

It's supposed to be a comma, not a period, between the two parameters.


Wednesday, February 1, 2017 7:18 PM

It's supposed to be a comma, not a period, between the two parameters.

What two parameters??  There is nothing here with two parameters.

\(ツ)_/