Skip to content

Instantly share code, notes, and snippets.

@sliceofbytes
Created March 19, 2024 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sliceofbytes/2d544078755684939a9608aac441e351 to your computer and use it in GitHub Desktop.
Save sliceofbytes/2d544078755684939a9608aac441e351 to your computer and use it in GitHub Desktop.
Set Mouse Speed Using Powershell
<#
.SYNOPSIS
Sets the mouse speed.
.DESCRIPTION
Sets the mouse speed via the SystemParametersInfo SPI_SETMOUSESPEED
and stores the speed in the registry
.PARAMETER Speed
Integer between 1 (slowest) and 20 (fastest). A value of 10 is the default.
.EXAMPLE
Sets the mouse speed to the defautl value.
PS C:\> Set-MouseSpeed -Speed 10
.INPUTS
System.int
.NOTES
See Get-MouseSpeed also.
.LINK
about_functions_advanced
.LINK
about_comment_based_help
.LINK
https://msdn.microsoft.com/en-us/library/ms724947(v=VS.85).aspx
#>
function Set-MouseSpeed() {
[cmdletbinding()]
Param(
[Parameter(ValueFromPipeline=$true)]
[ValidateRange(1,20)]
[int]
$Speed = 10
)
$MethodDefinition = @"
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, uint pvParam, uint fWinIni);
"@
$User32 = Add-Type -MemberDefinition $MethodDefinition -Name "User32Set" -Namespace Win32Functions -PassThru
$User32::SystemParametersInfo(0x0071,0,$Speed,0) | Out-Null
Set-ItemProperty -Path "HKCU:\Control Panel\Mouse" -Name MouseSensitivity -Value $Speed
}
<#
.SYNOPSIS
Gets the mouse speed.
.DESCRIPTION
Gets the mouse speed via the SystemParametersInfo SPI_GETMOUSESPEED
.EXAMPLE
Gets the current mouse speed.
PS C:\> Get-MouseSpeed
.Outputs
System.int
.NOTES
See Set-MouseSpeed also.
.LINK
about_functions_advanced
.LINK
about_comment_based_help
.LINK
https://msdn.microsoft.com/en-us/library/ms724947(v=VS.85).aspx
#>
function Get-MouseSpeed() {
$MethodDefinition = @"
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref uint pvParam, uint fWinIni);
"@
$User32 = Add-Type -MemberDefinition $MethodDefinition -Name "User32Get" -Namespace Win32Functions -PassThru
[Int32]$Speed = 0
$User32::SystemParametersInfo(0x0070,0,[ref]$Speed,0) | Out-Null
return $Speed
}
<#
Set the mouse speed
Example: PS C:\> MouseSpeed.ps1 10
#>
$NewValue = $args[0]
$OldValue = Get-MouseSpeed
Set-MouseSpeed -Speed $NewValue
Write-Host "Speed changed from $($OldValue) to $(Get-MouseSpeed)"
Function Set-MouseSpeed {
[CmdletBinding()]
param (
[validateRange(1, 20)]
[int] $Value
)
$winApi = add-type -name user32 -namespace tq84 -passThru -memberDefinition '
[DllImport("user32.dll")]
public static extern bool SystemParametersInfo(
uint uiAction,
uint uiParam ,
uint pvParam ,
uint fWinIni
);
'
$SPI_SETMOUSESPEED = 0x0071
$MouseSpeedRegPath = 'hkcu:\Control Panel\Mouse'
Write-Verbose "MouseSensitivity before WinAPI call: $((get-itemProperty $MouseSpeedRegPath).MouseSensitivity)"
$null = $winApi::SystemParametersInfo($SPI_SETMOUSESPEED, 0, $Value, 0)
#
# Calling SystemParametersInfo() does not permanently store the modification
# of the mouse speed. It needs to be changed in the registry as well
#
set-itemProperty $MouseSpeedRegPath -name MouseSensitivity -value $Value
Write-Verbose "MouseSensitivity after WinAPI call: $((get-itemProperty $MouseSpeedRegPath).MouseSensitivity)"
}
Set-MouseSpeed -Value 16 -Verbose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment