How to Get the Current Mouse Cursor Position in Batch Script
Getting the position of the mouse cursor is a very advanced task that steps beyond the traditional boundaries of batch scripting. A batch script, by its nature, is a text-based, non-graphical environment. It has no native, built-in commands to interact with or even be aware of the Graphical User Interface (GUI), including the mouse pointer.
This guide will explain this fundamental limitation and teach you the definitive and only reliable method for retrieving the mouse coordinates: calling a PowerShell one-liner from your batch script. You will learn the specific .NET Framework class required for this task and how to capture the X and Y coordinates into batch variables.
The Challenge: Batch Has No GUI Awareness
The cmd.exe interpreter operates in a text-based world. It understands files, processes, and text streams, but it has no concept of a desktop, windows, or a mouse cursor. There is no command like GET MOUSEPOS or a variable like %MOUSE_X%. To get this information, a batch script must delegate the task to a more powerful, GUI-aware scripting engine.
The Superior Method (Recommended): Using PowerShell
The correct and professional way to handle this is to call PowerShell, which can directly access the rich .NET Framework, including the System.Windows.Forms assembly that is aware of the GUI state.
This one-liner is the key. It loads the necessary assembly and then queries the cursor's position.
powershell -Command "Add-Type -AssemblyName System.Windows.Forms; $pos = [System.Windows.Forms.Cursor]::Position; Write-Host ('{0} {1}' -f $pos.X, $pos.Y)"
This command will output two numbers separated by a space: the X coordinate and the Y coordinate.
Basic Example: Displaying the Current Mouse Coordinates
This script simply executes the PowerShell command and shows the raw output.
@ECHO OFF
ECHO --- Getting the current mouse cursor position ---
ECHO Move your mouse around and run this script.
ECHO.
powershell -Command "Add-Type -AssemblyName System.Windows.Forms; $pos = [System.Windows.Forms.Cursor]::Position; Write-Host ('{0} {1}' -f $pos.X, $pos.Y)"
ECHO.
ECHO --- Done ---
The output will be the X and Y coordinates of the cursor at the moment the script was run.
--- Getting the current mouse cursor position ---
Move your mouse around and run this script.
1234 567
--- Done ---
How to Capture the X and Y Coordinates in Variables
To use these coordinates in your script, you need to capture the output of the PowerShell command into separate variables using a FOR /F loop.
@ECHO OFF
SET "MouseX="
SET "MouseY="
ECHO --- Capturing Mouse Coordinates ---
FOR /F "tokens=1,2" %%A IN (
'powershell -Command "Add-Type -AssemblyName System.Windows.Forms; $pos = [System.Windows.Forms.Cursor]::Position; Write-Host ('{0} {1}' -f $pos.X, $pos.Y)"'
) DO (
SET "MouseX=%%A"
SET "MouseY=%%B"
)
ECHO.
ECHO The captured mouse position is:
ECHO X Coordinate: %MouseX%
ECHO Y Coordinate: %MouseY%
How the PowerShell Method Works (A Step-by-Step Breakdown)
The PowerShell one-liner is a sequence of three commands:
Add-Type -AssemblyName System.Windows.Forms;: This is a critical prerequisite. The part of the .NET Framework that understands GUI elements like the cursor is not loaded into a default PowerShell session. This command loads the necessarySystem.Windows.Formsassembly into memory.$pos = [System.Windows.Forms.Cursor]::Position;: This is the core logic. It calls the staticPositionproperty of theCursorclass, which returns an object containing the cursor's X and Y screen coordinates. This object is stored in the$posvariable.Write-Host ('{0} {1}' -f $pos.X, $pos.Y): This formats the output for easy parsing. It creates a string, replacing{0}with theXproperty of the$posobject and{1}with theYproperty, separated by a space.
Common Pitfalls and How to Solve Them
Problem: Headless or Remote Desktop Sessions
If the script is run in a session that has no interactive desktop (e.g., a "headless" server session or certain types of remote connections), the concept of a mouse cursor may be undefined.
Solution: In these cases, the command will typically return 0 0. Your script should be prepared to handle this result if it's meant to run in such environments.
Practical Example: A "Wait for Mouse Movement" Script
This script demonstrates a practical use case. It checks the mouse position repeatedly and only continues once it detects that the user has moved the mouse, indicating they are present and active.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
TITLE Waiting for User Activity
ECHO --- Waiting for Mouse Movement ---
ECHO Please move the mouse to continue...
ECHO.
REM --- Get the initial mouse position ---
FOR /F "tokens=1,2" %%A IN ('powershell -c "Add-Type -A System.Windows.Forms;[System.Windows.Forms.Cursor]::Position.X;[System.Windows.Forms.Cursor]::Position.Y"') DO (
SET "InitialX=%%A"
SET "InitialY=%%B"
)
:WaitLoop
REM --- Get the current mouse position ---
FOR /F "tokens=1,2" %%A IN ('powershell -c "Add-Type -A System.Windows.Forms;[System.Windows.Forms.Cursor]::Position.X;[System.Windows.Forms.Cursor]::Position.Y"') DO (
SET "CurrentX=%%A"
SET "CurrentY=%%B"
)
REM --- Compare the positions ---
IF !CurrentX! NEQ !InitialX! GOTO :MovementDetected
IF !CurrentY! NEQ !InitialY! GOTO :MovementDetected
REM --- If no movement, wait and loop ---
TIMEOUT /T 2 /NOBREAK > NUL
GOTO :WaitLoop
:MovementDetected
ECHO.
ECHO [SUCCESS] Mouse movement detected.
ECHO The script will now proceed.
ENDLOCAL
A slightly different PowerShell command is used here for brevity inside the loop.
Conclusion
While batch scripting has no native ability to interact with the GUI, it can easily delegate this task to PowerShell, which is built into all modern versions of Windows.
Key takeaways:
- Do not attempt to get the mouse position with pure batch. It is not possible.
- The PowerShell
[System.Windows.Forms.Cursor]::Positionmethod is the definitive and recommended solution. - You must first load the required .NET assembly with
Add-Type -AssemblyName System.Windows.Forms. - Use a standard
FOR /Floop to capture the X and Y coordinates into batch variables.