Today, I tested this poweershell 7 version script:
# Check if script is running as Administrator
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Warning "Please run this script as Administrator."
exit
}
Write-Host "Starting system optimization..." -ForegroundColor Cyan
# Set execution policy
try {
Set-ExecutionPolicy RemoteSigned -Scope Process -Force
Write-Host "Execution policy set to RemoteSigned." -ForegroundColor Green
} catch {}
# Clean temporary files
try {
Remove-Item "$env:TEMP\*" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item "C:\Windows\Temp\*" -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "Temporary files cleaned." -ForegroundColor Green
} catch {}
# Set High Performance power plan
try {
powercfg -duplicatescheme SCHEME_MIN | Out-Null
powercfg -setactive SCHEME_MIN
Write-Host "High Performance power plan activated." -ForegroundColor Green
} catch {}
# Disable visual effects
try {
Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\VisualEffects" -Name "VisualFXSetting" -Value 2
Write-Host "Visual effects disabled." -ForegroundColor Green
} catch {}
# Disable unnecessary services
$services = @(
"DiagTrack", "WSearch", "Fax", "SysMain", "XblGameSave", "MapsBroker",
"RetailDemo", "dmwappushservice", "RemoteRegistry", "WerSvc", "WMPNetworkSvc"
)
foreach ($svc in $services) {
try {
Stop-Service -Name $svc -Force -ErrorAction SilentlyContinue
Set-Service -Name $svc -StartupType Disabled
Write-Host "Service '$svc' disabled." -ForegroundColor Green
} catch {
Write-Warning "Could not disable service '$svc': $_"
}
}
# Kill unwanted processes by name (PID cleanup)
$processesToKill = @("Cortana", "SkypeApp", "PeopleApp", "FeedbackHub", "SearchUI", "RuntimeBroker")
foreach ($proc in $processesToKill) {
try {
Get-Process -Name $proc -ErrorAction SilentlyContinue | Stop-Process -Force
Write-Host "Process '$proc' terminated." -ForegroundColor Green
} catch {
Write-Warning "Could not terminate process '$proc': $_"
}
}
# Optimize pagefile if RAM is 4GB or less
try {
$ramGB = [math]::Round((Get-CimInstance -ClassName Win32_ComputerSystem).TotalPhysicalMemory / 1GB)
if ($ramGB -le 4 -and (Get-Command wmic -ErrorAction SilentlyContinue)) {
Start-Process powershell -ArgumentList "-Command `"wmic computersystem where name='%computername%' set AutomaticManagedPagefile=False; wmic pagefileset where name='C:\\pagefile.sys' set InitialSize=4096,MaximumSize=8192`"" -Verb RunAs
Write-Host "Pagefile optimized for low RAM." -ForegroundColor Green
}
} catch {}
# Configure DNS using netsh
try {
$interfaces = Get-CimInstance -Namespace root/StandardCimv2 -ClassName MSFT_NetAdapter | Where-Object { $_.State -eq "Enabled" }
foreach ($iface in $interfaces) {
$name = $iface.Name
Write-Host "Configuring DNS for interface '$name'..."
Start-Process powershell.exe -ArgumentList "-Command `"netsh interface ip set dns name='$name' source=static addr=1.1.1.1`"" -Verb RunAs
Start-Process powershell.exe -ArgumentList "-Command `"netsh interface ip add dns name='$name' addr=8.8.8.8 index=2`"" -Verb RunAs
Write-Host "DNS configured for '$name'." -ForegroundColor Green
}
} catch {
Write-Warning "DNS configuration failed: $_"
}
# Legacy commands for PowerShell 5.1
$legacyCommands = @'
# Remove unwanted apps
$bloatware = @(
"Microsoft.3DBuilder", "Microsoft.XboxApp", "Microsoft.ZuneMusic",
"Microsoft.ZuneVideo", "Microsoft.BingWeather", "Microsoft.GetHelp",
"Microsoft.Getstarted", "Microsoft.People", "Microsoft.SkypeApp",
"Microsoft.WindowsFeedbackHub", "Microsoft.Cortana"
)
foreach ($app in $bloatware) {
try {
Get-AppxPackage -Name $app -AllUsers | Remove-AppxPackage -ErrorAction SilentlyContinue
Write-Host "Removed app: $app"
} catch {}
}
# Disable telemetry
try {
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\DataCollection" -Name "AllowTelemetry" -Value 0 -Force
Write-Host "Telemetry disabled."
} catch {}
# Enable SmartScreen
try {
Set-MpPreference -EnableSmartScreen $true
Write-Host "SmartScreen enabled."
} catch {}
# Enable firewall
try {
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True
Write-Host "Firewall enabled."
} catch {}
'@
# Save and run legacy script in PowerShell 5.1
try {
$tempScript = "$env:TEMP\legacy_opt.ps1"
if (Test-Path $tempScript) { Remove-Item $tempScript -Force }
Set-Content -Path $tempScript -Value $legacyCommands
Start-Process powershell.exe -ArgumentList "-ExecutionPolicy Bypass -File `"$tempScript`"" -Verb RunAs
Start-Sleep -Seconds 5
Remove-Item $tempScript -Force -ErrorAction SilentlyContinue
Write-Host "Legacy commands executed in PowerShell 5.1." -ForegroundColor Green
} catch {
Write-Warning "Failed to execute legacy commands: $_"
}
# Final message
Write-Host "`nSystem optimization completed successfully. A restart is recommended." -ForegroundColor Cyan
This is the result:
.\optimization_001.ps1
Starting system optimization...
Execution policy set to RemoteSigned.
Temporary files cleaned.
High Performance power plan activated.
Visual effects disabled.
Service 'DiagTrack' disabled.
Service 'WSearch' disabled.
Service 'Fax' disabled.
Service 'SysMain' disabled.
Service 'XblGameSave' disabled.
Service 'MapsBroker' disabled.
Service 'RetailDemo' disabled.
Service 'dmwappushservice' disabled.
Service 'RemoteRegistry' disabled.
Service 'WerSvc' disabled.
Service 'WMPNetworkSvc' disabled.
Process 'Cortana' terminated.
Process 'SkypeApp' terminated.
Process 'PeopleApp' terminated.
Process 'FeedbackHub' terminated.
Process 'SearchUI' terminated.
Process 'RuntimeBroker' terminated.
Pagefile optimized for low RAM.
Legacy commands executed in PowerShell 5.1.
System optimization completed successfully. A restart is recommended.