From 5ba6a48eac06d0dd82a0b3ea7eedc155190445ee Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Mon, 5 Aug 2024 20:07:39 +0200 Subject: [PATCH] Refactor windows configuration script --- scripts/Windows/Config/Windows/Install.ps1 | 2 +- scripts/Windows/Scripts/Registry.ps1 | 19 +++ scripts/Windows/Scripts/System.ps1 | 8 ++ scripts/Windows/Software/Windows/Manage.ps1 | 122 ++++++++++++++++++++ 4 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 scripts/Windows/Scripts/Registry.ps1 create mode 100644 scripts/Windows/Scripts/System.ps1 create mode 100644 scripts/Windows/Software/Windows/Manage.ps1 diff --git a/scripts/Windows/Config/Windows/Install.ps1 b/scripts/Windows/Config/Windows/Install.ps1 index f789a90c..ec0a8b1b 100644 --- a/scripts/Windows/Config/Windows/Install.ps1 +++ b/scripts/Windows/Config/Windows/Install.ps1 @@ -78,7 +78,7 @@ $newLayout = $startLayout | Select-Object -ExpandProperty $originalProperty | Wh } $startLayout.PSObject.Properties.Remove($originalProperty); -$startLayout | Add-Member -NotePropertyName "primaryOEMPins" -NotePropertyValue $newLayout; +$startLayout | Add-Member -NotePropertyName "originalProperty" -NotePropertyValue $newLayout; $startLayout | ConvertTo-Json -Compress | Set-Content "$env:SystemDrive\Users\Default\AppData\Local\Microsoft\Windows\Shell\LayoutModification.json"; diff --git a/scripts/Windows/Scripts/Registry.ps1 b/scripts/Windows/Scripts/Registry.ps1 new file mode 100644 index 00000000..767d3361 --- /dev/null +++ b/scripts/Windows/Scripts/Registry.ps1 @@ -0,0 +1,19 @@ +using namespace Microsoft.Win32; + +$null = New-Module { + function Edit-DefaultUserKey { + param( + [scriptblock] $Action + ) + + $rootPath = "HKLM:\DefaultUser"; + $regRootPath = $rootPath -replace "^(\w+):([\\/]|$)","`$1`$2"; + $hivePath = "$env:SystemDrive\Users\Default\NTUSER.dat"; + $null = & reg load $regRootPath $hivePath; + [RegistryKey] $root = Get-Item $rootPath; + & $Action -Key $root; + $root.Handle.Close(); + [System.GC]::Collect(); + & reg unload $regRootPath; + } +} diff --git a/scripts/Windows/Scripts/System.ps1 b/scripts/Windows/Scripts/System.ps1 new file mode 100644 index 00000000..33b473b4 --- /dev/null +++ b/scripts/Windows/Scripts/System.ps1 @@ -0,0 +1,8 @@ +<# + .SYNOPSIS + Creates a new temporary directory. +#> +function New-TemporaryDirectory { + $path = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName()); + New-Item -ItemType Directory $path; +} diff --git a/scripts/Windows/Software/Windows/Manage.ps1 b/scripts/Windows/Software/Windows/Manage.ps1 new file mode 100644 index 00000000..20a3b04a --- /dev/null +++ b/scripts/Windows/Software/Windows/Manage.ps1 @@ -0,0 +1,122 @@ +using namespace Microsoft.Win32; + +param( + $Action, + [hashtable] $Arguments +) + +. "$PSScriptRoot/../../Scripts/Registry.ps1"; +. "$PSScriptRoot/../../Scripts/Config.ps1"; +. "$PSScriptRoot/../../Scripts/System.ps1"; +. "$PSScriptRoot/../../Scripts/Software.ps1"; +. "$PSScriptRoot/../../Types/InstallerAction.ps1"; + +Start-SoftwareInstaller @PSBoundParameters ` + -Configurator { + $dir = New-TemporaryDirectory; + Push-Location $dir; + + function Get-WinConfig { + param( + [string] $Name + ) + + Get-Config "valhalla.windows.$Name"; + } + + if (Get-WinConfig "legacyIconSpacing") { + Write-Host "Setting up old-school Desktop Icon spacing"; + + Edit-DefaultUserKey { + param ( + [RegistryKey] $Key + ) + + $property = "IconSpacing"; + $relativePath = "Control Panel\Desktop\WindowMetrics" + $path = "$($Key.PSPath)\$relativePath"; + Copy-ItemProperty "HKCU:\$relativePath" $path $property; + Rename-ItemProperty $path $property "_$property"; + Set-ItemProperty $path -Name $property -Value "-1710" -Type "String"; + }; + } + + if (-not (Get-WinConfig "dynamicLighting")) { + Write-Host "Disabling Dynamic Lighting"; + + Edit-DefaultUserKey { + param ( + [RegistryKey] $Key + ) + + $path = "$($Key.PSPath)\Software\Microsoft\Lighting"; + New-Item $path; + Set-ItemProperty $path -Name "AmbientLightingEnabled" -Value 0 -Type "DWord"; + }; + } + + if (-not (Get-WinConfig "adware")) { + $startLayoutFile = "start.json"; + Write-Host "Removing adware"; + + Edit-DefaultUserKey { + param ( + [RegistryKey] $Key + ) + + $winKey = "$($Key.PSPath)\Software\Microsoft\Windows\CurrentVersion"; + $contentDeliveryKey = "$winKey\ContentDeliveryManager"; + $cloudContentKey = "HKLM:\Software\Policies\Microsoft\Windows\CloudContent"; + + foreach ($key in @($contentDeliveryKey, $cloudContentKey)) { + if (-not (Test-Path $key)) { + $null = New-Item $key; + } + } + + # Set-ItemProperty $cloudContentKey -Name "DisableWindowsConsumerFeatures" -Value 1 -Type "DWord"; + Set-ItemProperty $cloudContentKey -Name "DisableCloudOptimizedContent" -Value 1 -Type "DWord"; + Set-ItemProperty $cloudContentKey -Name "DisableConsumerAccountStateContent" -Value 1 -Type "DWord"; + + # Disabling Personal MS Teams Icon + Set-ItemProperty "$winKey\Explorer\Advanced" -Name "TaskBarMn" -Value 0 -Type "DWord"; + Set-ItemProperty $contentDeliveryKey -Name "ContentDeliveryAllowed" -Value 0 -Type "DWord"; + Set-ItemProperty $contentDeliveryKey -Name "SilentInstalledAppsEnabled" -Value 0 -Type "DWord"; + Set-ItemProperty $contentDeliveryKey -Name "SystemPaneSuggestionsEnabled" -Value 0 -Type "DWord"; + }; + + Write-Host "Removing ads from pinned apps"; + Export-StartLayout $startLayoutFile; + + $startLayout = Get-Content $startLayoutFile | ConvertFrom-Json; + $property = "pinnedList"; + + $newLayout = $startLayout | Select-Object -ExpandProperty $property | Where-Object { + -not ( + ($_.desktopAppLink -like "*Microsoft Edge*") -or + [System.Linq.Enumerable]::Any( + @( + "*MicrosoftOfficeHub*", + "*SpotifyMusic*", + "*WhatsApp*", + "*PrimeVideo*", + "*Netflix*", + "*Instagram*", + "*ChipChamp*", + "*Facebook*", + "*LinkedIn*"), + [System.Func[System.Object,bool]]{ + param($pattern) + $_.packagedAppId -like "$pattern"; + })); + }; + + $startLayout.PSObject.Properties.Remove($property); + $startLayout | Add-Member -NotePropertyName $property -NotePropertyValue $newLayout; + + $startLayout | ConvertTo-Json -Compress | Set-Content "$env:SystemDrive\Users\Default\AppData\Local\Microsoft\Windows\Shell\LayoutModification.json"; + } + + Pop-Location; + Remove-Item -Recurse $dir; + };