Problem in Powershell not consuming ReadKey

398 Views Asked by At

When using the following LOC in a custom Powershell function that essentially was a "Wait until any key is pressed" called from a script:

[System.Console]::ReadKey($true)

… later on, particularly when the function returned or completed its context I got:

KeyChar   Key Modifiers
-------   --- ---------
... Enter         0

Q: How to avoid this??

1

There are 1 best solutions below

2
David Jones On

I found the solution was that you need to consume the ReadKey call. i.e.:

  $keypress = [System.Console]::ReadKey($true)

I found the same with some other PowerShell calls, that did an action but returned a value, such as a count, that the returned value was eventually displayed. Yes I got unused warnings but I ignored them.

Hope this helps someone. It annoyed me for several days off an on until I realised this.