Uncover the infinite in IT

Table of Contents
< All Topics

Mouse Movement Simulator for Windows

This is a detailed step-by-step tutorial on how to create, configure, and use the MoveMouse.ps1 PowerShell script to simulate mouse movement until a specific time.

Step-by-Step Tutorial

Step 1: Open PowerShell as Administrator

  1. Press the Windows key.
  2. Type “PowerShell”.
  3. Right-click on “Windows PowerShell” and select “Run as administrator”.

Step 2: Change the Execution Policy

To allow running scripts, you need to change the execution policy:

1) In the PowerShell window, check the current execution policy:

Get-ExecutionPolicy

Output Example:

Restricted

2) Set the execution policy to RemoteSigned to allow running locally created scripts:

Set-ExecutionPolicy RemoteSigned

Confirm the change by typing Y and pressing Enter when prompted.

Step 3: Create the PowerShell Script

1) Open a text editor (e.g., Notepad).

2) Copy and paste the following script into the text editor:

# Add .NET class for mouse_event
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class MouseSimulator {
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
    private const int MOUSEEVENTF_MOVE = 0x0001;
    public static void MoveMouse(int xDelta, int yDelta) {
        mouse_event(MOUSEEVENTF_MOVE, (uint)xDelta, (uint)yDelta, 0, 0);
    }
}
"@

# Add the required assemblies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

function Move-RandomMouse {
    # Get screen dimensions
    $screenWidth = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width
    $screenHeight = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height

    # Generate random coordinates within the screen size
    $x = Get-Random -Minimum 0 -Maximum $screenWidth
    $y = Get-Random -Minimum 0 -Maximum $screenHeight

    # Move the mouse to the generated coordinates
    [System.Windows.Forms.Cursor]::Position = [System.Drawing.Point]::new($x, $y)

    # Simulate a small mouse move to ensure the system detects activity
    [MouseSimulator]::MoveMouse(1, 1)
    Start-Sleep -Milliseconds 50
    [MouseSimulator]::MoveMouse(-1, -1)
}

# Loop to keep moving the mouse every 30 seconds until 18:00 hours
while ($true) {
    $currentTime = Get-Date
    if ($currentTime.Hour -ge 18) {
        Write-Output "Stopping script as the time is now 18:00 or later."
        break
    }
    Move-RandomMouse
    Start-Sleep -Seconds 30
}

3) Save the file with the name MoveMouse.ps1 on your desktop or any preferred location.

Step 4: Run the Script

1) In the PowerShell window (running as administrator), navigate to the location where you saved the script. For example, if you saved it on your desktop, you would use:

cd C:\Users\YourUsername\Desktop

2) Run the script:

.\MoveMouse.ps1

Step 5: Understand What the Script Does

Loading .NET Assemblies and Types:

  • The script adds a .NET class to simulate mouse movement by calling the mouse_event function from user32.dll.
  • It also loads the necessary assemblies for using System.Windows.Forms and System.Drawing.

Move-RandomMouse Function:

  • Retrieves the screen dimensions using System.Windows.Forms.SystemInformation.
  • Generates random coordinates within the screen dimensions.
  • Moves the mouse to these coordinates using System.Windows.Forms.Cursor::Position.
  • Simulates a small mouse movement to ensure the system detects activity.

Main Loop:

  • The script enters an infinite loop (while ($true)).
  • In each iteration, it checks the current time using Get-Date.
  • If the time is 18:00 or later, it stops the script with a message.
  • Otherwise, it calls Move-RandomMouse to move the mouse and waits for 30 seconds before repeating.

Step 6: Output Examples

When running the script, you might see output similar to this in the PowerShell window:

Stopping script as the time is now 18:00 or later.

This indicates that the script has successfully run until the specified time and then stopped.

Stopping the Script Manually

If you need to stop the script before 18:00 hours, you can do so by pressing Ctrl + C in the PowerShell window.

Reverting the Execution Policy

After running the script, you might want to revert the execution policy to its original state:

  1. Open PowerShell as administrator.
  2. Run the following command:
Set-ExecutionPolicy Restricted

Confirm the change by typing Y and pressing Enter when prompted.

Summary

This guide walks you through enabling script execution, creating a PowerShell script to simulate mouse movement, running the script, and understanding its functionality. The script ensures the system stays active by simulating mouse activity every 30 seconds until 18:00 hours.