50

I have ps1 script to grab some information from the vmware cluster environment.

In some place of ps1 script requires the ENTER button keystroke.

So, How to do that ?

-Thanks

3
  • 1
    Which is the script i wrote, requires the ENTER keystroke. instead of hitting enter button all the time, simply i want to automate. Jul 25, 2013 at 6:16
  • Why does it require pressing enter? Are you executing read-host?
    – Keith Hill
    Jul 25, 2013 at 6:31
  • Myscript continuosly login/logout from several machine. By the time of logout it requires confirmation, so i have to hit enter button. This is the reason i want automate. Jul 25, 2013 at 7:12

5 Answers 5

76

If I understand correctly, you want PowerShell to send the ENTER keystroke to some interactive application?

$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('title of the application window')
Sleep 1
$wshell.SendKeys('~')

If that interactive application is a PowerShell script, just use whatever is in the title bar of the PowerShell window as the argument to AppActivate (by default, the path to powershell.exe). To avoid ambiguity, you can have your script retitle its own window by using the title 'new window title' command.

A few notes:

  • The tilde (~) represents the ENTER keystroke. You can also use {ENTER}, though they're not identical - that's the keypad's ENTER key. A complete list is available here: http://msdn.microsoft.com/en-us/library/office/aa202943%28v=office.10%29.aspx.
  • The reason for the Sleep 1 statement is to wait 1 second because it takes a moment for the window to activate, and if you invoke SendKeys immediately, it'll send the keys to the PowerShell window, or to nowhere.
  • Be aware that this can be tripped up, if you type anything or click the mouse during the second that it's waiting, preventing to window you activate with AppActivate from being active. You can experiment with reducing the amount of time to find the minimum that's reliably sufficient on your system (Sleep accepts decimals, so you could try .5 for half a second). I find that on my 2.6 GHz Core i7 Win7 laptop, anything less than .8 seconds has a significant failure rate. I use 1 second to be safe.
  • IMPORTANT WARNING: Be extra careful if you're using this method to send a password, because activating a different window between invoking AppActivate and invoking SendKeys will cause the password to be sent to that different window in plain text!

Sometimes wscript.shell's SendKeys method can be a little quirky, so if you run into problems, replace the fourth line above with this:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('~');
2
  • 3
    Thanks for that Add-Type addition to the end there. Was banging my head against the wall for an hour trying to get my script working. Mar 23, 2015 at 20:04
  • What are the differences between using Wscript.Shell and using SendKeys.SendWait? Is one better that the other?
    – tfpf
    Jul 22, 2021 at 20:29
3
function Do-SendKeys {
    param (
        $SENDKEYS,
        $WINDOWTITLE
    )
    $wshell = New-Object -ComObject wscript.shell;
    IF ($WINDOWTITLE) {$wshell.AppActivate($WINDOWTITLE)}
    Sleep 1
    IF ($SENDKEYS) {$wshell.SendKeys($SENDKEYS)}
}
Do-SendKeys -WINDOWTITLE Print -SENDKEYS '{TAB}{TAB}'
Do-SendKeys -WINDOWTITLE Print
Do-SendKeys -SENDKEYS '%{f4}'
1

Send "Enter" key to an App, for example for pressing "OK". Works great:

Add-Type -AssemblyName microsoft.VisualBasic

Add-Type -AssemblyName System.Windows.Forms

# Get the desired process:

$ProcessName = Get-Process -Name Calculator

Start-Sleep -Seconds 1

# If the process is actually running, bring it to front:

If ($ProcessName)
{
   (New-Object -ComObject.Wscript.Shell).AppActivate((Get-Process $ProcessName -ErrorAction SilentlyContinue).MainWindowTitle)
}

# Send "Enter" key to the app:

[Microsoft.VisualBasic.Interaction]::AppActivate($ProcessName.ProcessName)
[System.Windows.Forms.SendKeys]::SendWait({'~'})
0

Also the $wshell = New-Object -ComObject wscript.shell; helped a script that was running in the background, it worked fine with just but adding $wshell. fixed it from running as background! [Microsoft.VisualBasic.Interaction]::AppActivate("App Name")

-2

For remote desktop connection after user name and password authentication it is asking for warning message per company policy, the enter command is not accepting to it, how to give ok to it

2
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    May 14 at 20:40
  • 1
    If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review May 18 at 19:56

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.