Screen Shot 2022-07-24 at 11.52.33 AMScreen Shot 2022-07-24 at 11.52.33 AMScreen Shot 2022-07-24 at 11.52.33 AMScreen Shot 2022-07-24 at 11.52.33 AM
  • Contact
  • Series
    • PowerShell Q&A
  • Blog
Contact NOW
✕

Automate Cursor Movement on Windows with PowerShell

  • Home
  • Blog
  • Getting Started With PowerShell
  • Automate Cursor Movement on Windows with PowerShell
powershell-icon
PowerShell Modules: Extending the Capabilities of Your Scripts
02/27/2023
How to Integrate ChatGPT into a FiveM Server
03/17/2023
Published by Service Account on 03/13/2023
Categories
  • Getting Started With PowerShell
  • PowerShell
Tags
  • analytics
  • Automation
  • C#
  • cursor movement
  • data skills
  • PowerShell
  • programming
  • SEO
  • technical skills
  • Windows
Automate Cursor Movement on Windows with PowerShell
Automating Cursor Movement on Windows Screen with PowerShell

Introduction:

PowerShell is a powerful automation tool with the ability to automate various repetitive tasks. In this technical blog post, we explain how to use PowerShell to begin to Automate Cursor Movement on the Windows screen. Moving the cursor to specific locations is useful for simulating user input, controlling presentations, or other tasks requiring cursor movement. We will cover the necessary libraries and methods required to automate cursor movement in PowerShell. Our step-by-step guide will show you how to import the required System.Windows.Forms and user32.dll libraries and define a C# class and method to easily move the cursor on the screen within your PowerShell script. After reading this post, you’ll be equipped with the knowledge to automate cursor movement on a Windows screen using PowerShell.

Section 1: Automate Cursor Movement Loading Libraries and Defining Class and Method

# PowerShell script documentation

## Section 1: Load System.Windows.Forms and user32.dll libraries

```powershell
Add-Type -AssemblyName System.Windows.Forms
Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    public static class Win32 {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetCursorPos(int x, int y);
    }
"@

The purpose of this section is to load the System.Windows.Forms and user32.dll libraries and define a class and method that will allow us to move the cursor on the screen.

The first line of this section uses the Add-Type cmdlet with the -AssemblyName parameter to load the System.Windows.Forms library into the PowerShell session. The System.Windows.Forms library grants access to numerous Windows Forms controls and graphical user interface components that PowerShell scripts can use to create user interfaces.

The second line of this section uses the Add-Type cmdlet with a here-string to define a C# class named Win32 that contains a method named SetCursorPos. This method is declared using the DllImport attribute to import the SetCursorPos function from the user32.dll library. The SetCursorPos function is a native Win32 API function that can be used to set the position of the cursor on the screen.

The DllImport attribute is used to specify the name of the DLL that contains the function (user32.dll), the calling convention (StdCall is the default), and the return type of the function (bool). The [return: MarshalAs(UnmanagedType.Bool)] attribute is used to specify that the bool return value should be marshalled as an unmanaged boolean value.

By defining this class and method in C# and importing it into PowerShell using the Add-Type cmdlet, we can use the SetCursorPos method to move the cursor on the screen from within our PowerShell script.

Section 2: Automate Cursor Movement Define Move-CursorFigureEight function

function Move-CursorFigureEight {
    $centerX = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Width / 2
    $centerY = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Height / 2
    $radiusX = 200
    $radiusY = 100
    $angle = 0

    while ($true) {
        $x = $centerX + ($radiusX * [math]::Sin($angle))
        $y = $centerY + ($radiusY * [math]::Sin(2 * $angle))

        [Win32]::SetCursorPos([int]$x, [int]$y)

        $angle += 0.1

        Start-Sleep -Milliseconds 10
    }
}

The purpose of this function is to move the cursor on the screen in a figure-eight pattern. It does this by using trigonometric functions to calculate the x and y coordinates of the cursor based on the current angle of rotation.

The function starts by defining several variables. $centerX and $centerY are calculated using the System.Windows.Forms.Screen class to find the center of the primary screen. $radiusX and $radiusY control the size of the figure-eight pattern, and $angle is initialized to 0.

The function then enters a while loop that runs indefinitely. On each iteration of the loop, it calculates the new position of the cursor based on the current value of $angle. The formula used to calculate the new position is as follows:

$x = $centerX + ($radiusX * [math]::Sin($angle))
$y = $centerY + ($radiusY * [math]::Sin(2 * $angle))

Here, $x and $y are calculated using the Sin function from the math class. $x is based on the value of $angle, while $y is based on 2 * $angle. By using these two values together, we create a figure-eight pattern.

The function then calls the SetCursorPos method from the Win32 class that we defined earlier. This method takes two arguments, $x and $y, and moves the cursor to the specified position on the screen.

Finally, the function increments $angle by 0.1 on each iteration of the loop to control the speed of the movement, and calls the Start-Sleep cmdlet to pause the script for 10 milliseconds between each iteration.

By defining this function, we can call it to start moving the cursor in a figure-eight pattern on the screen. The size and speed of the pattern can be adjusted by changing the values of $radiusX, $radiusY, and $angle.

Section 3: Call Move-CursorFigureEight function

Move-CursorFigureEight

This section calls the Move-CursorFigureEight function to start moving the cursor in a figure-eight pattern.

Section 4: Automate Cursor Movement Complete Code

Here’s the function all together

Add-Type -AssemblyName System.Windows.Forms
# Load user32.dll library
Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    public static class Win32 {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetCursorPos(int x, int y);
    }
"@

# Define figure eight function
function Move-CursorFigureEight {
    $centerX = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Width / 2
    $centerY = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Height / 2
    $radiusX = 200
    $radiusY = 100
    $angle = 0

    while ($true) {
        $x = $centerX + ($radiusX * [math]::Sin($angle))
        $y = $centerY + ($radiusY * [math]::Sin(2 * $angle))

        [Win32]::SetCursorPos([int]$x, [int]$y)

        $angle += 0.1

        Start-Sleep -Milliseconds 10
    }
}

# Call figure eight function
Move-CursorFigureEight

We hope you found this blog post helpful in automating cursor movement on a Windows screen using PowerShell. If you enjoyed this content, we invite you to check out our other technical blog posts. Our blog covers a range of topics related to PowerShell, Bash, C# Azure, Groovy. With a focus on data, analytics, technical, programming, and speaking and writing ability. With that we conclude this post on Automating Cursor Movement

Our step-by-step guide shows how to use PowerShell to move the cursor on the screen in a figure-eight pattern. Import the necessary System.Windows.Forms and user32.dll libraries and define a C# class and method to move the cursor on the screen within your PowerShell script.

Explore our blog for more technical content and stay updated on the latest automation trends and best practices in the industry.

Powershell Loops Part 2: While, and Do-Until

PowerShell Functions: Organizing Your Scripts and Reusing Code

Share
Service Account
Service Account

Related posts

powershell-icon
02/27/2023

PowerShell Modules: Extending the Capabilities of Your Scripts


Read more
02/20/2023

PowerShell Functions: Organizing Your Scripts and Reusing Code


Read more
02/13/2023

PowerShell Hashtables: Storing and Manipulating Data


Read more

Comments are closed.

© Automate and Deploy · Made with | ChenzoDesigns.com
    Contact NOW