This PowerShell script does the following:
- It checks if the script is running with administrator privileges. If not, it restarts itself with elevated privileges.
- It disables the safe services in Windows 11.
- The script attempts to stop each service and set its startup type to "Disabled".
- It cleans temp files from the Windows Temp folder and the user's Temp folder.
- It disables fast boot by modifying the registry.
- It sets the power plan to balanced using the powercfg command.
Code:
# Elevate privileges if not already running as administrator
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Exit
}
# 1. Disable safe services in Windows 11
$servicesToDisable = @(
"AssignedAccessManager",
"BitLocker",
"DiagTrack", # Connected User Experiences and Telemetry
"DusmSvc", # Data Usage
"lfsvc", # Geolocation Service
"MapsBroker", # Downloaded Maps Manager
"Netlogon",
"defragsvc", # Optimize Drives
"WpcMonSvc", # Parental Controls
"PhoneSvc",
"Spooler", # Print Spooler
"SessionEnv", # Remote Desktop Configuration
"TermService", # Remote Desktop Services
"UmRdpService", # Remote Desktop Services UserMode Port Redirector
"SensorService",
"ScDeviceEnum", # Smart Card Device Enumeration Service
"SCardSvr", # Smart Card
"ScPolicySvc", # Smart Card Removal Policy
"WbioSrvc", # Windows Biometric Service
"WerSvc", # Windows Error Reporting Service
"workfolderssvc", # Work Folders
"XblAuthManager", # Xbox Live Auth Manager
"XblGameSave", # Xbox Live Game Save
"XboxNetApiSvc", # Xbox Live Networking Service
"WaaSMedicSvc", # Windows Update Medic Service
"Fax",
"WalletService",
"icssvc", # Windows Mobile Hotspot Service
"TabletInputService", # Touch Keyboard and Handwriting Panel Service
"DevicesFlowUserSvc", # DevicePicker
"AJRouter" # AllJoyn Router Service
)
foreach ($service in $servicesToDisable) {
$serviceObj = Get-Service -Name $service -ErrorAction SilentlyContinue
if ($serviceObj) {
Stop-Service -Name $service -Force -ErrorAction SilentlyContinue
Set-Service -Name $service -StartupType Disabled
Write-Host "Disabled service: $service"
} else {
Write-Host "Service not found: $service"
}
}
# 2. Clean all temp files
$tempFolders = @(
"C:\Windows\Temp\*",
"$env:TEMP\*"
)
foreach ($folder in $tempFolders) {
Remove-Item -Path $folder -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "Cleaned temp files in: $folder"
}
# 3. Disable fast boot
$path = "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Power"
$name = "HiberbootEnabled"
Set-ItemProperty -Path $path -Name $name -Value 0
Write-Host "Disabled fast boot"
# 4. Set power plan to balanced
powercfg /setactive 381b4222-f694-41f0-9685-ff5bb260df2e
Write-Host "Set power plan to balanced"
Write-Host "Script execution completed."
To use this script:
- Save it as a .ps1 file (e.g., optimize_windows11.ps1).
- Right-click on the file and select "Run with PowerShell".
- If prompted, allow the script to run with administrator privileges.
Please note that disabling services and changing system settings can have unintended consequences. It's recommended to create a system restore point before running this script, so you can revert changes if needed. Also, some services might be required for certain features or hardware on your specific system, so make sure you understand the implications of disabling each service before running the script.