Monday, December 15, 2025

How to get x axis and y axis position of Mouse cursor in Powershell

 # Load the necessary assemblies for accessing mouse position information

Add-Type -AssemblyName System.Drawing

Add-Type -AssemblyName System.Windows.Forms


Write-Host "Move your mouse. Press Ctrl+C to stop."


while ($true) {

    # Get the current mouse position

    $cursorPosition = [System.Windows.Forms.Cursor]::Position

    $x = $cursorPosition.X

    $y = $cursorPosition.Y

    

    # Print the coordinates. Using Write-Host with -NoNewline and carriage return `r` 

    # overwrites the previous line in the console for a cleaner continuous update.

    Write-Host "`rX: $x | Y: $y" -NoNewline

    

    # Wait for a short period to prevent the loop from consuming too much CPU

    Start-Sleep -Milliseconds 100

}



or for single position



Add-Type -AssemblyName System.Windows.Forms

$mousePosition = [System.Windows.Forms.Cursor]::Position

Write-Host "Current mouse position is X: $($mousePosition.X), Y: $($mousePosition.Y)"

No comments:

Post a Comment