A PowerShell module for Windows that provides programmatic control over mouse position, clicks, scrolling, and smooth cursor movement with natural-looking bezier curves.
This module started as a simple script to monitor my daughter's computer usage. I wanted to know when she'd been on the computer too long so I could generate a notification. But once I started playing with P/Invoke through PowerShell, I couldn't resist implementing more functionality.
- OS: Windows
- PowerShell: 5.1 or 7+
- Copy
PSMouseControl.psm1to your PowerShell modules directory, or - Import the module directly:
Import-Module .\PSMouseControl.psm1
- Get/Set mouse position with pixel-perfect accuracy
- Click left, right, or middle mouse buttons
- Scroll vertically and horizontally
- Plot smooth paths between points with natural-looking bezier curves
- Natural mouse movement with automatic acceleration/deceleration
- Monitor mouse position in real-time
Gets the current mouse cursor position.
$pos = Get-MousePosition
Write-Host "X: $($pos.X), Y: $($pos.Y)"Sets the mouse cursor position to specific coordinates.
Set-MousePosition -X 500 -Y 300
# Or use pipeline input
New-MousePosition 500 300 | Set-MousePositionCreates a new PSMouseControl.Point object representing screen coordinates.
$point = New-MousePosition -X 100 -Y 200Generates a smooth, natural-looking path between two points using bezier curves with automatic acceleration and deceleration. The control point is automatically generated with a random perpendicular offset from the direct path.
$start = New-MousePosition 100 700
$end = New-MousePosition 2000 125
# Move the mouse smoothly along the path
New-MousePath -Start $start -End $end | ForEach-Object {
$_ | Set-MousePosition
Start-Sleep -Milliseconds 10
}Features:
- Automatic control point generation for natural curves
- Ease-in/ease-out (cubic) animation for smooth acceleration/deceleration
- Random curve variation to avoid repetitive patterns
Simulates a mouse button click.
# Left click (default)
Invoke-MouseClick
# Right click
Invoke-MouseClick -Button Right
# Middle click
Invoke-MouseClick -Button MiddleValid buttons: Left, Right, Middle
Simulates mouse wheel scrolling in any direction.
# Scroll up
Invoke-MouseScroll -Direction Up
# Scroll down
Invoke-MouseScroll -Direction Down
# Horizontal scroll left
Invoke-MouseScroll -Direction Left
# Horizontal scroll right
Invoke-MouseScroll -Direction RightValid directions: Up, Down, Left, Right
Continuously monitors and outputs the mouse position. Stops when the mouse reaches coordinates (0, 0).
# Monitor with default 10ms interval
Watch-MousePosition
# Monitor with custom interval
Watch-MousePosition -Interval ([timespan]::FromMilliseconds(100))
# With verbose output
Watch-MousePosition -VerboseImport-Module .\PSMouseControl.psm1
$start = Get-MousePosition
$end = New-MousePosition 1920 1080
New-MousePath -Start $start -End $end | ForEach-Object {
Set-MousePosition -X $_.X -Y $_.Y
Start-Sleep -Milliseconds 10
}Set-MousePosition -X 500 -Y 300
Invoke-MouseClick -Button LeftWatch-MousePosition | ForEach-Object {
Write-Host "Current position: $($_.X), $($_.Y)"
}# Move to button location
$buttonPos = New-MousePosition 640 480
New-MousePath -Start (Get-MousePosition) -End $buttonPos | ForEach-Object {
$_ | Set-MousePosition
Start-Sleep -Milliseconds 8
}
# Click the button
Invoke-MouseClick -Button Left
Start-Sleep -Seconds 1
# Scroll down to see more content
Invoke-MouseScroll -Direction DownThe module uses P/Invoke to call Windows User32.dll functions:
GetCursorPos- Get cursor positionSetCursorPos- Set cursor positionmouse_event- Simulate mouse clicks and scrollingSetProcessDPIAware- Ensure accurate positioning on high-DPI displays
The New-MousePath function creates natural-looking mouse movements using:
- Quadratic Bezier Curves: Generates a curved path instead of straight lines
- Automatic Control Point: Creates a perpendicular offset (10-25% of distance) from the direct path
- Ease-In/Ease-Out: Cubic easing function makes the cursor accelerate smoothly at the start and decelerate at the end
- Randomization: Each path varies slightly to avoid robotic patterns