6

using powershell code i would like to open the application and click on any option. like open Notepad++ and click on file -->new

This is independent script which run in powershell console

i tried to grab the Notepad++ window handle like this

$WindowHandle = Get-Process | Where-Object { $_.MainWindowTitle -Match $WindowTitle } | Select-Object -ExpandProperty MainWindowHandle

but i am not able to click on it.

to open Notepad++

start-process "C:\Program Files\Notepad++\notepad++.exe"

to grab the window

 $WindowHandle = Get-Process | Where-Object { $_.MainWindowTitle -Match $WindowTitle } | Select-Object -ExpandProperty MainWindowHandle

any code snippet which can help me in grabbing the control on notepad++ and click on options or buttons.

1 Answer 1

3

If all the actions you want to do have a shortcut, you could just send the keys to the window.

For example to open a new file you can use CTRL + N

start-process "C:\Program Files\Notepad++\notepad++.exe"
$WindowHandle = Get-Process | Where-Object { $_.MainWindowTitle -Match $WindowTitle } | Select-Object -ExpandProperty MainWindowHandle

[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[System.Windows.Forms.SendKeys]::SendWait("^{n}")

EDIT: When you press ALT in Notepad++ it will show you the shortcuts you can use to open the different menus.

enter image description here

To open File, you can use ALT+F (The F is underlined, meaning that's the shortcut). After that you could navigate down/up using keys to select what you want. Most applications will have this behavior.

Besides that, in Notepad++ you could also map pretty much any action to a key combination. However, this would mean your script only works on your PC, with your settings.

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the code. I need one help..if its not with ctrl + n if i want to click on any button in application is that possible.?
When you press the ALT key in notepad++ it will show shortcuts for all the menu tabs. Most applications have this enabled (or should have :) ). I'll edit my answer with what I mean.
what's the mechanism by which [System.Windows.Forms.SendKeys]::SendWait("^{n}") knows to send that key to the $WindowHandle window? Is it looking for a variable in the local environment explicitly named $WindowHandle?
@DWR: It doesn't know, it sends to the window that has focus. If you open another application while running this, that application will get the key strokes.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.