Mouse Click automation with powershell (or other non-external software)

20,722

Assuming you seek either a Mouse Recorder (aka. Mouse Macro, or similar).

Comparably, I achieve what you seek with Windows PowerShell ISE, which needs not the hassle of having to d/l & instal: open a blank project in PowerShell ISE and add the following code...

First, you need to set up your environment

  1. Setting up an environment by importing a couple of system assemblies — simply copy these two

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
    
  2. Create an array by calling in user32.dll & calling the mouse clicking event within. Note: this is important if you wish to send mouse-clicking events. Without this, your mouse will just keep on changing its position but won't click anywhere. (More information can be found here.)

    $signature=@'
    [DllImport("user32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
    public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
    '@
    
  3. You will now have to add a type of the above called mouse event array list.

    $SendMouseClick = Add-Type -memberDefinition $signature -name "Win32MouseEventNew" -namespace Win32Functions -passThru
    

Your environment is ready.

Now, how to use it

  1. Get mouse/cursor pixel position on your screen by entering below command.

    $X = [System.Windows.Forms.Cursor]::Position.X
    $Y = [System.Windows.Forms.Cursor]::Position.Y
    Write-Output "X: $X | Y: $Y"
    

    In above command you are simply getting cursor X & Y Position. Please note, you may have to perform this steps for more times to get the mouse pixels, as in where you want your mouse cursor to go and click.

  2. Once you have all your positions noted down where you wish to perform the mouse clicks, it's now time to set it up.

    $x = 86
    $y = 172
    [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y)
    sleep -Seconds 01
    $SendMouseClick::mouse_event(0x00000002, 0, 0, 0, 0);
    $SendMouseClick::mouse_event(0x00000004, 0, 0, 0, 0);
    

Now, once you have all the $X & $Y positions ready where you need to perform the clicks, edit and repeat the above code in PowerShell ISE. Each time, replace $X & $Y values with the values you wish to hover or to click to.

$SendMouseClick::mouse_event(0x00000002, 0, 0, 0, 0); will perform left click down.

$SendMouseClick::mouse_event(0x00000004, 0, 0, 0, 0); will perform left click up.

So if you wish to perform a double click, simply call the lines twice, so something like this.

$SendMouseClick::mouse_event(0x00000002, 0, 0, 0, 0);
$SendMouseClick::mouse_event(0x00000004, 0, 0, 0, 0);
$SendMouseClick::mouse_event(0x00000002, 0, 0, 0, 0);
$SendMouseClick::mouse_event(0x00000004, 0, 0, 0, 0);

This may sound an outdated process, but it does the job. I find it very useful where no other third-party tools are available for me to use. As you pointed it out, PowerShell comes pre-installed.

Share:
20,722
Tim D
Author by

Tim D

Updated on February 21, 2020

Comments

  • Tim D
    Tim D about 4 years

    Some of my tasks are very repetitive, sometimes I have to click the same pattern for an hour.

    This could easily done by an mouse recorder, but unfortunately I am not able to download anything. I have PowerShell preinstalled on my computer, so this could be an option (I've used this successfully in the past for making directories).

    My question is thus how I can make a PowerShell mouse recorder (and then automate it in loop) or using other preinstalled software. I'm running windows 7.