"Apply" changes to the registry to change mouse settings without reboot on Windows

46 Views Asked by At

Objective

I am attempting to change the cursor schema with a powershell script avoiding reboots or log outs.

That is basically a big resume but, to have a context, I am learning about Windows OS and the PowerShell language... yes, that means that I have low knowledge about this OS (I am more comfortable in Linux :')). To learn this language I decided to make a script which change some OS settings and is the time for the cursor schema. I attempted to change the language (UI and internal), and also needs a reboot (I guess it's almost the same).

Actual Code

I have this function after lots of modifications:

function changeMouseScheme {
        function updateRegistry {

            $imagePath = "C:\Windows\Media\Resources\Images\newCursor.jpg"

            $regConnect = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"CurrentUser","$env:COMPUTERNAME")
            $regCursors = $regConnect.OpenSubKey("Control Panel\Cursors",$true)

            $regCursors.SetValue("","Windows Black")
            $regCursors.SetValue("AppStarting", $imagePath)
            $regCursors.SetValue("Arrow", $imagePath)
            $regCursors.SetValue("Crosshair", $imagePath)
            $regCursors.SetValue("Hand", "")
            $regCursors.SetValue("Help", $imagePath)
            $regCursors.SetValue("IBeam", $imagePath)
            $regCursors.SetValue("No", $imagePath)
            $regCursors.SetValue("NWPen", $imagePath)
            $regCursors.SetValue("SizeAll", $imagePath)
            $regCursors.SetValue("SizeNESW",$imagePath)
            $regCursors.SetValue("SizeNS", $imagePath)
            $regCursors.SetValue("SizeNWSE", $imagePath)
            $regCursors.SetValue("SizeWE", $imagePath)
            $regCursors.SetValue("UpArrow", $imagePath)
            $regCursors.SetValue("Wait", $imagePath)
            $regCursors.Close()
            $regConnect.Close()

            Write-Output "[+] Mouse registry updated"
        }
        
        function applyChanges {
            $code = @'
                [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
                public static extern bool SystemParametersInfo( uint uiAction, uint uiParam, uint pvParam, uint fWinIni);
                
                public static void applyRegistry() {
                    SystemParametersInfo(0x0057, 0, "", 0x01 | 0x02);
                }
'@
            Add-Type -MemberDefinition $code -Name applyMouseRegistry -Namespace User32
            [User32.applyMouseRegistry]::applyRegistry()

            Write-Output "[+] Mouse registry applied"
        }

        updateRegistry
        applyChanges

        Write-Output "[+] Mouse scheme changed"
    }

But when executing it, the cursor does not change.

Observed resources (no one worked)

I did a deep search on these pages but nothing worked:

https://devblogs.microsoft.com/scripting/use-powershell-to-change-the-mouse-pointer-scheme/
https://superuser.com/questions/1769195/how-to-change-mouse-cursor-using-powershell-script-on-windows-11-without-restart
How to change wall paper by using powershell
Change and update the size of the cursor in Windows 10 via PowerShell

Expected result

When the script is executed, the cursor schema changes without some kind of reboot / log out / shutdown, etc, etc...

0

There are 0 best solutions below