1

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??

David Jones
  • 542
  • 4
  • 13

1 Answers1

1

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.

David Jones
  • 542
  • 4
  • 13
  • I hope the [answer](https://stackoverflow.com/a/55665963/45375) to the linked duplicate question explains why that is necessary, and how to simply _discard_ return values that aren't of interest. – mklement0 Feb 14 '20 at 03:21
  • 1
    The answer link above IS relevant, – David Jones Feb 15 '20 at 10:52