Home > OS >  Windows Forms SendKeys using Read-Host
Windows Forms SendKeys using Read-Host

Time:09-20

Apologies if this question has already been answered on another thread, but I went looking for my specific query and I couldn't anything.

The issue I have is sending keystrokes to a Read-Host command. When I do this, nothing populates. Is this even possible?

$sendKeys = [System.Windows.Forms.SendKeys]
Sleep-Start -Seconds 1
$sendKeys::SendWait('serv01{ENTER}')
Read-Host -Prompt "Server Name"

Running this would only show the prompt and still waiting for input:

Server Name: 

Please advise if this is possible.

CodePudding user response:

Your code:

  • DOES work in regular console windows, in Windows Terminal, and - assuming the keyboard focus is on the integrated terminal when the code is run - in Visual Studio Code.

  • does NOT work in the Windows PowerShell ISE.

Note: The workaround suggested for use in the ISE may actually be the preferable solution overall, given that sending keystrokes is inherently brittle (not fully robust).

  • Related