1

Is it possible to get powershell to write something in a window, meaning where ever the mouse lands, it writes it in that window, if it's in the web browser an application etc?

I'm thinking of trying to make a script which would read what is stated in the request and then go from there.

It would look something like this:

$test = "write this text"

Add-Type -TypeDefinition $cSource -ReferenceAssemblies System.Windows.Forms,System.Drawing

  Start-Sleep -Seconds 1

[Clicker]::LeftClickAtPoint(900,900)

  Start-Sleep -Seconds 1
Write-Content "$test"

I can't find out if there exists a cmdlet for this.

0

1 Answer 1

2

What you are asking for here is GUI automation. This is not the target use case for PS. So, there is no built-in cmdlet for this and no cmdlet or function called Write-Content. The closet you can get on your own is to use the old SendKeys option.

# Send output to Notepad and view it wihtout creating a file first.
# Start notepad
Notepad 

# Get a list of servics and put on the clipboard
Get-Service | Clip

# Put the clipboard content in the notepad instance
## .NET:
[void][reflection.assembly]::loadwithpartialname("system.windows.forms")
[system.windows.forms.sendkeys]::sendwait('^v')

However, that is really not reliable, because different cpu, graphic cards and really mess with trying to use it consistently across platforms. Been there, done that, and still do it for small localhost’s where I absolutely know how it is going to flow.

There are modules you can download and use to assist with GUI automation …

https://archive.codeplex.com/?p=wasp

https://archive.codeplex.com/?p=uiautomation

… and are 3rdP tools like AutoIT.

All that being said, you’ll still have the issue of working to figure out what window you are trying to hit, UI element positions, etc. That sort of thing requires C# and other API calls to make sure you can get to that stuff.

There are numerous discussions about this sort of effort all over the web, and the pain / failure / successes experienced along the way. Just do a search for ‘PowerShell SendKeys’ or ‘PowerShell GUI automation’ and you’ll see this right away.

Lastly, all this GUI stuff, where you are trying to deal with mouseclick events, mouseover events, mousehover events, etc., is really only possible for an application you have personally written, in most cases, unless the app providers exposed / gave you API info to do this.

Here is a SO discussion that highlights what I am saying here and should be of interest to you.

How i can send mouse click in powershell

https://stackoverflow.com/questions/39353073/how-i-can-send-mouse-click-in-powershell

Update per comment / request

No worries.

Codeplex has been around for a long while, but is not longer getting any activity. Hence the archive stuff. All of the stuff on Codeplex has been copied / hosted to GitHub and the like locations.

Which module are you after to load?

Ideally, you want this in you user profile module location...

C:\Users\YourUserName\Documents\WindowsPowerShell\Modules

.. but you can put it in both.

Sometimes, I have module not autoload as expected, So, I just manually load them via profile configuration or in the script that will use the module.

Import-Module -Name ModuleName

Or

Import-Module -Name C:\Users\YourUserName\Documents\WindowsPowerShell\Modules\ModuleName
3
  • Thank you, never heard of codeplex before. Do you know how you get the module "installed"? I tried a few guides (just extracing them to "C:\Windows\System32\WindowsPowerShell\v1.0\Modules" but when I try to write "get-module -listavailable" in PowerShell it doesn't show the modules? I also tried to restart my computer. Not sure what i'm doing wrong
    – B.Frederik
    Jul 1, 2018 at 16:40
  • NO worries.Which module are you after to load.
    – postanote
    Jul 1, 2018 at 20:25
  • I tried to install both of your examples (Wasp and Uiautomation)
    – B.Frederik
    Jul 2, 2018 at 5:12

Your Answer

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.