Last active
October 5, 2023 05:28
-
-
Save Smalls1652/bca1f2535b45a2c4201993ee230a9c08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding(SupportsShouldProcess)] | |
param( | |
[Parameter(Position = 0)] | |
[ValidateSet("Up", "Down")] | |
[string]$ScrollDirection = "Down", | |
[Parameter(Position = 1)] | |
[switch]$ForAllNewProfiles | |
) | |
begin { | |
#The registry key path and property that controls which direction touchpad scrolling operates. | |
switch ($ForAllNewProfiles) { | |
$true { | |
$touchpadScrollReg = [pscustomobject]@{ | |
"RegPath" = "HKLM:\DefaultProfile\SOFTWARE\Microsoft\Windows\CurrentVersion\PrecisionTouchPad"; | |
"Name" = "ScrollDirection"; | |
"Type" = "DWORD"; | |
} | |
break | |
} | |
Default { | |
$touchpadScrollReg = [pscustomobject]@{ | |
"RegPath" = "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\PrecisionTouchPad"; | |
"Name" = "ScrollDirection"; | |
"Type" = "DWORD"; | |
} | |
break | |
} | |
} | |
#Defining the two values for the direction | |
$scrollOptions = @( | |
[pscustomobject]@{ | |
"ScrollDownDirection" = "Up"; | |
"Value" = 0x0; | |
}, | |
[pscustomobject]@{ | |
"ScrollDownDirection" = "Down"; | |
"Value" = 0xffffffff; | |
} | |
) | |
} | |
process { | |
$selectedOption = $null | |
switch ($ScrollDirection) { | |
"Up" { | |
#If the scroll direction is set to "Up" | |
Write-Verbose "Selected option is to scroll up." | |
$selectedOption = $scrollOptions | Where-Object { $PSItem.ScrollDownDirection -eq "Up" } | |
break | |
} | |
Default { | |
#If the scroll direction is set to "Down" | |
Write-Verbose "Selected option is to scroll down." | |
$selectedOption = $scrollOptions | Where-Object { $PSItem.ScrollDownDirection -eq "Down" } | |
break | |
} | |
} | |
#Creating a splat that can be used by both 'New-ItemProperty' and 'Set-ItemProperty'. | |
$itemPropertySplat = @{ | |
"Path" = $touchpadScrollReg.RegPath; | |
"Name" = $touchpadScrollReg.Name; | |
"Value" = $selectedOption.Value; | |
"ErrorAction" = "Stop"; | |
} | |
switch ($ForAllNewProfiles) { | |
$true { | |
#If the script is running to modify the default profile's registry hive | |
Write-Warning "Setting the option in the default profile's registry hive." | |
if ($PSCmdlet.ShouldProcess("Default Profile", "Load registry hive")) { | |
#Create a temporary file to redirect the output of the 'reg' command | |
$tmpFile = New-TemporaryFile | |
#Get the file path for the default profile's registry hive. | |
$defaultRegHivePath = "$($env:SystemDrive)\Users\Default\NTUSER.DAT" | |
#Load the default profile's registry hive | |
Start-Process -FilePath "reg" -ArgumentList @("load", "HKLM\DefaultProfile", "$($defaultRegHivePath)") -Wait -NoNewWindow -RedirectStandardOutput $tmpFile.FullName | |
} | |
break | |
} | |
Default { | |
#Else, do nothing | |
break | |
} | |
} | |
#Change the scroll direction | |
if ($PSCmdlet.ShouldProcess("Scroll direction", "Change scroll direction to $($selectedOption.ScrollDownDirection.ToLower())")) { | |
try { | |
$null = New-ItemProperty @itemPropertySplat -PropertyType $touchpadScrollReg.Type | |
} | |
catch [System.IO.IOException] { | |
#If the property already exists, then just set it. | |
$null = Set-ItemProperty @itemPropertySplat | |
} | |
} | |
switch ($ForAllNewProfiles) { | |
$true { | |
#If the script is running to modify the default profile's registry hive | |
if ($PSCmdlet.ShouldProcess("Default Profile", "Unload registry hive")) { | |
#In order to successfully unload the loaded registry hive, we need to do garbage collection | |
[System.GC]::Collect() | |
#Unload the registry hive | |
Start-Process -FilePath "reg" -ArgumentList @("unload", "HKLM\DefaultProfile") -Wait -NoNewWindow -RedirectStandardOutput $tmpFile.FullName | |
#Remove the temp file | |
$tmpFile | Remove-Item -Force | |
} | |
Write-Warning "Changes will not have an effect on users who have already logged in. This will only apply to new users who log into this computer." | |
break | |
} | |
Default { #Else, show the warning about when the setting will take effect | |
Write-Warning "In order for changes to take effect, you must log out and then log back in." | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment