<# .SYNOPSIS Windows tweaks script for general improvements over windows defaults .DESCRIPTION - Enforces various windows tweaks to improve windows runtime in an opinionated way .EXAMPLE # Run script locally C:\PS> .\Windows-11-Tweaks.ps1 # Run script remotely C:\PS> Set-ExecutionPolicy -ExecutionPolicy Bypass C:\PS> iex "& { $(irm https://wobig.tech/downloads/scripts/Windows-11-Tweaks.ps1) }" .NOTES Author: Rick Wobig Source: https://wobig.tech/downloads/scripts/Windows-11-Tweaks.ps1 #> [CmdletBinding()] param ( [Parameter(Mandatory = $false, HelpMessage = "Power plan to enforce by name, is case sensitive")] [ValidateNotNullOrEmpty()] [string]$DesiredPowerPlan = "Ultimate Performance", [Parameter(Mandatory = $false, HelpMessage = "Testing argument, will show verbose output and pause at certain script locations")] [Switch] [bool]$Testing = $false, [Parameter(Mandatory = $false, HelpMessage = "Root directory where logs are generated/cleaned up")] [ValidateNotNullOrEmpty()] [string]$PathLogs = "$([System.Environment]::GetEnvironmentVariable('TEMP','Machine'))\", [Parameter(Mandatory = $false, HelpMessage = "File prefix for generated files like logs, used for consistency and cleanup")] [ValidateNotNullOrEmpty()] [string]$ScriptFilePrefix = "Win-11-Tweaks_" ) #region Script Execution try { # Instantiate Logger and indicate the script has started [Logger]::new($Testing, $PathLogs, $ScriptFilePrefix) [Logger]::Logger.LogDisplay("Starting script execution", "Cyan") [WindowsGeneral]::DisableWindowsWebSearch() [WindowsGeneral]::EnableAllTaskbarNotifications() [Network]::DisableNetworkThrottling() [Graphics]::IncreaseGraphicTdr() [PowerPlan]::EnforcePowerPlan($DesiredPowerPlan) [Hpet]::DisableHpet() # Finish up [Logger]::Logger.LogDisplay("Stopping script execution", "Cyan") } catch { [HouseKeeping]::StopOnFailure("GLOBAL FAILURE: $($PSItem.ToString())") } #endregion # region Classes class Logger { static [Logger] $Logger [bool] hidden $TestMode [string] hidden $LogPath [string] hidden $FilePrefix [string] hidden $LogDate = "$(Get-Date -Format %M_%d)" Logger( [bool]$testing, [string]$pathLogs, [string]$filePrefix ) { $this.TestMode = $testing $this.LogPath = $pathLogs $this.FilePrefix = $filePrefix [Logger]::Logger = $this [Logger]::GenerateLogDir($this.LogPath) } Log($message) { $methodName = [Logger]::GetMethodName(2) Add-Content "$($this.LogPath)$($this.FilePrefix)$($this.LogDate).log" "[$(Get-Date -UFormat %H:%M:%S)] $($methodName): $($message)" if ($this.TestMode) { Write-Host -fore DarkGray "[$(Get-Date -UFormat %H:%M:%S)] $($methodName): $($message)" } } LogPause($message) { if ($this.TestMode) { $methodName = [Logger]::GetMethodName(2) Add-Content "$($this.LogPath)$($this.FilePrefix)$($this.LogDate).log" "[$(Get-Date -UFormat %H:%M:%S)] $($methodName): $($message)" Write-Host -fore DarkGray "[$(Get-Date -UFormat %H:%M:%S)] $($methodName): $($message)" Pause } } LogDisplay($message, $color = "Gray"){ $methodName = [Logger]::GetMethodName(2) Add-Content "$($this.LogPath)$($this.FilePrefix)$($this.LogDate).log" "[$(Get-Date -UFormat %H:%M:%S)] $($methodName): $($message)" if ($this.TestMode){ Write-Host -fore $color "[$(Get-Date -UFormat %H:%M:%S)] $($methodName): $($message)" return } Write-Host -fore $color "[$(Get-Date -UFormat %H:%M:%S)] $($message)" } LogLoading() { if (!($this.TestMode)) { Add-Content "$($this.LogPath)$($this.FilePrefix)$($this.LogDate).log" "." Write-Host -fore DarkGray -NoNewline "." } } static [string] GetMethodName([int]$StackNumber = 1) { return [string]$(Get-PSCallStack)[$StackNumber].FunctionName.ToUpper() } static [string] GenerateLogDir($logDir) { try { if (!(Test-Path -Path $logDir)) { New-Item $logDir -ItemType Directory } return $logDir } catch { Write-Host -fore Red "FAILURE: $($PSItem.ToString())" return $null } } } class HouseKeeping { static [void] FinishScriptExecution($startTime){ [Logger]::Logger.LogDisplay("Script Finished", "Cyan") $endTime = Get-Date [Logger]::Logger.LogDisplay("Total script time: $(($endTime - $startTime).TotalSeconds)", "Cyan") Exit } static [void] StopOnFailure($exitMessage) { if ($null -ne [Logger]::Logger) { [Logger]::Logger.LogDisplay("$exitMessage | Stopping script execution", "Red") } Exit } } class IO { static [bool] RemoveFolderIfExist($folderPath){ try { if (Test-Path -PathType Container $folderPath) { Remove-Item $folderPath -Recurse -Force -ErrorAction SilentlyContinue [Logger]::Logger.Log("Removed folder recursively: $folderPath") return $true } else { [Logger]::Logger.Log("Folder doesn't exist, skipping removal: $folderPath") return $true } } catch { [Logger]::Logger.LogDisplay("FAILURE: $($PSItem.ToString())", "Red") return $false } } static [bool] CreateFolderIfNotExist($folderPath){ # Create folder path if it doesn't exist if (!(Test-Path -PathType Container $folderPath)) { try { New-Item -Path $folderPath -ItemType "directory" | Out-Null [Logger]::Logger.Log("Created non-existant folder: $folderPath") return $true } catch { [Logger]::Logger.LogDisplay("FAILURE: $($PSItem.ToString())", "Red") return $false } } return $false } } class WindowsGeneral { static [void] DisableWindowsWebSearch(){ try { Set-ItemProperty -Path 'HKCU:\SOFTWARE\Policies\Microsoft\Windows' -Name 'DisableSearchBoxSuggestions' -Value '1' -Type Dword [Logger]::Logger.LogDisplay("Successfully disabled web search in windows search", "Green") } catch { [Logger]::Logger.LogDisplay("Failed to disable web search in windows search", "Red") } } static [void] EnableAllTaskbarNotifications(){ try { Set-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer' -Name 'EnableAutoTray' -Value '0' -Type Dword [Logger]::Logger.LogDisplay("Successfully enabled all taskbar notifications", "Green") } catch { [Logger]::Logger.LogDisplay("Failed to enable all taskbar notifications", "Red") } } } class Graphics { # Configure TDR delay for rendering hangs | TDR == Timeout Detection and Recovery static [void] IncreaseGraphicTdr(){ try { Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\GraphicsDrivers' -Name 'TdrDelay' -Value '60' -Type Dword Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\GraphicsDrivers' -Name 'TdrDdiDelay' -Value '60' -Type Dword [Logger]::Logger.LogDisplay("Successfully adjusted graphics TDR to a reasonable level", "Green") } catch { [Logger]::Logger.LogDisplay("Failed to adjust graphics TDR to a reasonable level", "Red") } } } class Network { static [void] DisableNetworkThrottling(){ try { Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile' -Name 'NetworkThrottlingIndex' -Value '0xffffffff' -Type Dword [Logger]::Logger.LogDisplay("Successfully disabled network throttling", "Green") } catch { [Logger]::Logger.LogDisplay("Failed to disable network throttling", "Red") } } } class Hpet { static [void] DisableHpet(){ $hpetSuccessMessage = "operation completed successfully" $disableHpetClock = (bcdedit /set useplatformclock no) $disableHpetTick = (bcdedit /set useplatformtick yes) $disableHpetDynamicTick = (bcdedit /set disabledynamictick yes) if ($disableHpetClock.Contains($hpetSuccessMessage) -and $disableHpetTick.Contains($hpetSuccessMessage) -and $disableHpetDynamicTick.Contains($hpetSuccessMessage) ){ [Logger]::Logger.LogDisplay("Successfully disabled HPET", "Green") return } [Logger]::Logger.LogDisplay("Failed to disabled HPET", "Red") } } class PowerPlan { static [System.Collections.Generic.List[string]] GetPowerPlans(){ $powerPlans = (powercfg /list | Select-String 'GUID' | ForEach-Object { $_ -replace '.*:\s+([^\s]+)\s*$', '$1' }) return $powerPlans } static [string] GetCurrentPowerPlan(){ $currentPowerPlan = (powercfg /getactivescheme | Select-String 'GUID' | ForEach-Object { $_ -replace '.*:\s+([^\s]+)\s*$', '$1' }) return $currentPowerPlan } static [string] GetPowerPlanGuid($powerPlanName){ $powerPlans = [PowerPlan]::GetPowerPlans() foreach ($plan in $powerPlans) { if ($plan.ToLower().Contains($powerPlanName.ToLower())) { $pattern += "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}" $matches = [regex]::Matches($plan, $pattern) return $matches[0] } } return $null } static [bool] EnforceUltimatePerformancePlan(){ $powerPlans = [PowerPlan]::GetPowerPlans() foreach ($plan in $powerPlans){ if ($plan.ToLower().Contains("ultimate performance")){ return $true } } $powerPlanConfig = (powercfg -duplicatescheme e9a42b02-d5df-448d-aa00-03f14749eb61) if ($powerPlanConfig.Contains('(Ultimate Performance)')){ return $true } return $false } static [bool] SetPowerPlan($powerPlanGuid){ $setPowerPlan = (powercfg /setactive $powerPlanGuid) if (-not ($setPowerPlan -eq $null)){ return $false } return $true } static [void] EnforcePowerPlan($desiredPowerPlan){ if ($desiredPowerPlan -eq "Ultimate Performance") { $ultimateEnforced = [PowerPlan]::EnforceUltimatePerformancePlan() if ($ultimateEnforced){ [Logger]::Logger.LogDisplay("Successfully enforced the ultimate performance power plan", "Green") } else { [Logger]::Logger.LogDisplay("Failed to enforce the ultimate performance power plan", "Red") } } $powerPlanGuidDesired = [PowerPlan]::GetPowerPlanGuid($desiredPowerPlan) $powerPlanSet = [PowerPlan]::SetPowerPlan($powerPlanGuidDesired) if ($powerPlanSet){ $currentPowerPlan = [PowerPlan]::GetCurrentPowerPlan() [Logger]::Logger.LogDisplay("Desired power plan configured:", "Green") [Logger]::Logger.LogDisplay(" $currentPowerPlan", "Green") } else { [Logger]::Logger.LogDisplay("Failed to configure the desired power plan", "Red") } } } # endregion