Skip to content

Instantly share code, notes, and snippets.

@AScriver
Forked from halkyon/cleanup-win10.ps1
Last active March 18, 2023 18:59
Show Gist options
  • Save AScriver/9b768c97fe7cf401845e41da31baccea to your computer and use it in GitHub Desktop.
Save AScriver/9b768c97fe7cf401845e41da31baccea to your computer and use it in GitHub Desktop.
Cleanup Windows 10 Powershell script
## Instructions
## 1. Run this script (under Powershell as Administrator):
## powershell -ExectionPolicy Bypass .\cleanup-win10.ps1
## 2. Let it run through, you may see a few errors, this is normal
## 3. Reboot
## shutdown /r /t 0
## 5. Done!
##
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
Write-Host 'Updating registry settings...'
# Disable some of the "new" features of Windows 10, such as forcibly installing apps you don't want, and the new annoying animation for first time login.
New-Item -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\' -Name 'CloudContent'
New-ItemProperty -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\CloudContent' -Name 'DisableWindowsConsumerFeatures' -PropertyType DWord -Value '1' -Force
New-ItemProperty -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\CloudContent' -Name 'DisableSoftLanding' -PropertyType DWord -Value '1' -Force
New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System' -Name 'EnableFirstLogonAnimation' -PropertyType DWord -Value '0' -Force
# Set some commonly changed settings for the current user. The interesting one here is "NoTileApplicationNotification" which disables a bunch of start menu tiles.
New-Item -Path 'HKCU:\Software\Policies\Microsoft\Windows\CurrentVersion\' -Name 'PushNotifications'
New-ItemProperty -Path 'HKCU:\Software\Policies\Microsoft\Windows\CurrentVersion\PushNotifications' -Name 'NoTileApplicationNotification' -PropertyType DWord -Value '1' -Force
New-Item -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\' -Name 'CabinetState'
New-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\CabinetState' -Name 'FullPath' -PropertyType DWord -Value '1' -Force
New-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced' -Name 'HideFileExt' -PropertyType DWord -Value '0' -Force
New-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced' -Name 'Hidden' -PropertyType DWord -Value '1' -Force
New-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced' -Name 'ShowSyncProviderNotifications' -PropertyType DWord -Value '0' -Force
# Remove all Windows 10 apps, except the Windows Store
Get-AppxProvisionedPackage –Online | Where-Object {$_.packagename –NotLike "*store*"} | Remove-AppxProvisionedPackage -Online
Get-AppxPackage -AllUsers | Where-Object {$_.name –NotLike "*store*"} | Remove-AppxPackage
# Disable Cortana, and disable any kind of web search or location settings.
New-Item -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\' -Name 'Windows Search'
New-ItemProperty -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\Windows Search' -Name 'AllowCortana' -PropertyType DWord -Value '0' -Force
New-ItemProperty -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\Windows Search' -Name 'AllowSearchToUseLocation' -PropertyType DWord -Value '0' -Force
New-ItemProperty -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\Windows Search' -Name 'DisableWebSearch' -PropertyType DWord -Value '1' -Force
New-ItemProperty -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\Windows Search' -Name 'ConnectedSearchUseWeb' -PropertyType DWord -Value '0' -Force
# Remove OneDrive, and stop it from showing in Explorer side menu.
C:\Windows\SysWOW64\OneDriveSetup.exe /uninstall
Remove-Item -Path 'HKCR:\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6}' -Recurse
Remove-Item -Path 'HKCR:\Wow6432Node\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6}' -Recurse
# Disable data collection and telemetry settings.
New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer' -Name 'SmartScreenEnabled' -PropertyType String -Value 'Off' -Force
New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\DataCollection' -Name 'AllowTelemetry' -PropertyType DWord -Value '0' -Force
New-ItemProperty -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\DataCollection' -Name 'AllowTelemetry' -PropertyType DWord -Value '0' -Force
# Disable Windows Defender submission of samples and reporting.
New-Item -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows Defender\' -Name 'Spynet'
New-ItemProperty -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows Defender\Spynet' -Name 'SpynetReporting' -PropertyType DWord -Value '0' -Force
New-ItemProperty -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows Defender\Spynet' -Name 'SubmitSamplesConsent' -PropertyType DWord -Value '2' -Force
# Ensure updates are downloaded from Microsoft instead of other computers on the internet.
New-Item -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\' -Name 'DeliveryOptimization'
New-ItemProperty -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\DeliveryOptimization' -Name 'DODownloadMode' -PropertyType DWord -Value '0' -Force
New-ItemProperty -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\DeliveryOptimization' -Name 'SystemSettingsDownloadMode' -PropertyType DWord -Value '0' -Force
New-Item -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\DeliveryOptimization\' -Name 'Config'
New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\DeliveryOptimization\Config' -Name 'DODownloadMode' -PropertyType DWord -Value '0' -Force
Write-Host 'Disabling services...'
$services = @(
# See https://virtualfeller.com/2017/04/25/optimize-vdi-windows-10-services-original-anniversary-and-creator-updates/
# Connected User Experiences and Telemetry
'DiagTrack',
# Data Usage service
'DusmSvc',
# Peer-to-peer updates
'DoSvc',
# AllJoyn Router Service (IoT)
'AJRouter',
# SSDP Discovery (UPnP)
'SSDPSRV',
'upnphost',
# Superfetch
'SysMain',
# http://www.csoonline.com/article/3106076/data-protection/disable-wpad-now-or-have-your-accounts-and-private-data-compromised.html
'iphlpsvc',
'WinHttpAutoProxySvc',
# Black Viper 'Safe for DESKTOP' services.
# See http://www.blackviper.com/service-configurations/black-vipers-windows-10-service-configurations/
'tzautoupdate',
'AppVClient',
'RemoteRegistry',
'RemoteAccess',
'shpamsvc',
'SCardSvr',
'UevAgentService',
'ALG',
'PeerDistSvc',
'NfsClnt',
'dmwappushservice',
'MapsBroker',
'lfsvc',
'HvHost',
'vmickvpexchange',
'vmicguestinterface',
'vmicshutdown',
'vmicheartbeat',
'vmicvmsession',
'vmicrdv',
'vmictimesync',
'vmicvss',
'irmon',
'SharedAccess',
'MSiSCSI',
'SmsRouter',
'CscService',
'SEMgrSvc',
'PhoneSvc',
'RpcLocator',
'RetailDemo',
'SensorDataService',
'SensrSvc',
'SensorService',
'ScDeviceEnum',
'SCPolicySvc',
'SNMPTRAP',
'TabletInputService',
'WFDSConSvc',
'FrameServer',
'wisvc',
'icssvc',
'WinRM',
'WwanSvc',
'XblAuthManager',
'XblGameSave',
'XboxNetApiSvc'
)
foreach ($service in $services) {
Set-Service $service -StartupType Disabled
}
Write-Host 'Disabling hibernate...'
powercfg -h off
# Disables all of the known enabled-by-default optional features. There are some particulary bad defaults like SMB1. Sigh.
Write-Host 'Disabling optional features...'
$features = @(
'MediaPlayback',
'SMB1Protocol',
'Xps-Foundation-Xps-Viewer',
'WorkFolders-Client',
'WCF-Services45',
'NetFx4-AdvSrvs',
'Printing-Foundation-Features',
'Printing-PrintToPDFServices-Features',
'Printing-XPSServices-Features',
'MSRDC-Infrastructure',
'MicrosoftWindowsPowerShellV2Root',
'Internet-Explorer-Optional-amd64'
)
foreach ($feature in $features) {
Disable-WindowsOptionalFeature -Online -FeatureName $feature -NoRestart
}
Write-Host -Message 'Cleaning up Windows image.'
cmd.exe /c "DISM /Online /Cleanup-image /Restorehealth "
cmd.exe /c "DISM /Online /Cleanup-image /StartComponentCleanup /ResetBase"
Write-Host -Message 'Repairing system files.'
#c
# Empty recycle bin
Write-Host -Message 'Empty Recycle Bin.'
(New-Object -ComObject Shell.Application).Namespace(0xA).items() | % { rm $_.path -Recurse -Confirm:$false }
# Remove Windows temporary files
Write-Host -Message 'Deleting Windows temporary files.'
Remove-Item "c:\Windows\Temp\*" -Recurse -Force -ErrorAction SilentlyContinue
# Delete User temp files
Write-Host -Message 'Deleting User temporary files.'
Remove-Item "C:\Users\*\Appdata\Local\Temp\*" -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "Removing node_modules folders."
Get-ChildItem -Path "D://Code/*" -Include "node_modules" -Recurse -Directory | Remove-Item -Recurse -Force
Write-Host "Cleaning updates."
Stop-Service -Name wuauserv -Force
Remove-Item c:\Windows\SoftwareDistribution\Download\* -Recurse -Force
Start-Service -Name wuauserv
Write-Host "Cleaning SxS."
Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase
@(
"$env:localappdata\Nuget",
"$env:localappdata\temp\*",
"$env:windir\logs",
"$env:windir\panther",
"$env:windir\temp\*"
) | % {
if (Test-Path $_) {
Write-Host "Removing $_"
try {
Takeown /d Y /R /f $_
Icacls $_ /GRANT:r administrators:F /T /c /q 2>&1 | Out-Null
Remove-Item $_ -Recurse -Force | Out-Null
}
catch { $global:error.RemoveAt(0) }
}
}
$sections = @(
'Active Setup Temp Folders',
'BranchCache',
'Content Indexer Cleaner',
'Device Driver Packages',
'Downloaded Program Files',
'GameNewsFiles',
'GameStatisticsFiles',
'GameUpdateFiles',
'Internet Cache Files',
'Memory Dump Files',
'Offline Pages Files',
'Old ChkDsk Files',
'Previous Installations',
'Recycle Bin',
'Service Pack Cleanup',
'Setup Log Files',
'System error memory dump files',
'System error minidump files',
'Temporary Files',
'Temporary Setup Files',
'Temporary Sync Files',
'Thumbnail Cache',
'Update Cleanup',
'Upgrade Discarded Files',
'User file versions',
'Windows Defender',
'Windows Error Reporting Archive Files',
'Windows Error Reporting Queue Files',
'Windows Error Reporting System Archive Files',
'Windows Error Reporting System Queue Files',
'Windows ESD installation files',
'Windows Upgrade Log Files'
)
Write-Host -Message 'Clearing CleanMgr.exe automation settings.'
$getItemParams = @{
Path = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*'
Name = 'StateFlags0001'
ErrorAction = 'SilentlyContinue'
}
Get-ItemProperty @getItemParams | Remove-ItemProperty -Name StateFlags0001 -ErrorAction SilentlyContinue
Write-Host -Message 'Adding enabled disk cleanup sections...'
foreach ($keyName in $sections) {
$newItemParams = @{
Path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\$keyName"
Name = 'StateFlags0001'
Value = 1
PropertyType = 'DWord'
ErrorAction = 'SilentlyContinue'
}
$null = New-ItemProperty @newItemParams
}
Write-Host -Message 'Starting CleanMgr.exe...'
Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' -NoNewWindow -Wait
Write-Verbose -Message 'Waiting for CleanMgr and DismHost processes...'
Get-Process -Name cleanmgr, dismhost -ErrorAction SilentlyContinue | Wait-Process
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment