Skip to content

Instantly share code, notes, and snippets.

@mrzcn
Created December 13, 2020 10:46
Show Gist options
  • Select an option

  • Save mrzcn/4e23018808195fcd4c66730796b7926b to your computer and use it in GitHub Desktop.

Select an option

Save mrzcn/4e23018808195fcd4c66730796b7926b to your computer and use it in GitHub Desktop.
CleanMgr / Disk Cleanup Utuility ile Windows Disk Temizleme Scripti
# Ce script couvre mes serveurs Windows 2008 | 2008r2 | 2012 | 2012r2
# Version Powershell 2.0 & 3.0
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force
$VersionNumber = "1.1"
#Gestion du FLAG
$VersionKeyLocation = test-path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\MYCORP"
$VersionKEY = test-path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\MYCORP\$VersionNumber"
# Si le dossier n'existe pas, le créer.
IF ($VersionKeyLocation -eq $False){
New-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\" -Name "MYCORP" -Type Key -Force
}
# Si la clef avec la version spécifiée plus haut n'existe pas, je fais toute les actions, sinon, le script ne fait rien.
IF ($VersionKEY -eq $False){
New-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\MYCORP\" -Name "$VersionNumber" -Type Key -Force
# Mise en place des variables #
#---------------------------------------------------------------------------------------
# Windows Server 2008 32 bits en-US
$Path_exe_2008_US = "C:\Windows\winsxs\x86_microsoft-windows-cleanmgr_31bf3856ad364e35_6.0.6001.18000_none_6d4436615d8bd133\cleanmgr.exe"
# -Destination "C:\Windows\System32\"
$Path_mui_2008_US = "C:\Windows\winsxs\x86_microsoft-windows-cleanmgr.resources_31bf3856ad364e35_6.0.6001.18000_en-us_5dd66fed98a6c5bc\cleanmgr.exe.mui"
# -Destination "C:\Windows\System32\en-US"
#---------------------------------------------------------------------------------------
# Windows Server 2008 32 bits fr-FR
$Path_exe_2008_FR = "C:\Windows\winsxs\x86_microsoft-windows-cleanmgr_31bf3856ad364e35_6.0.6001.18000_none_6d4436615d8bd133\cleanmgr.exe"
# -Destination "C:\Windows\System32\"
$Path_mui_2008_FR = "C:\Windows\winsxs\x86_microsoft-windows-cleanmgr.resources_31bf3856ad364e35_6.0.6001.18000_fr-fr_005942d08b9fcdc3\cleanmgr.exe.mui"
# -Destination "C:\Windows\System32\fr-FR"
#----------------------------------------------------------------------------------------
# Windows Server 2008r2 64 bits fr-FR
$Path_exe_2008R2_FR = "C:\Windows\winsxs\amd64_microsoft-windows-cleanmgr_31bf3856ad364e35_6.1.7600.16385_none_c9392808773cd7da\cleanmgr.exe"
# -Destination "C:\Windows\System32\"
$Path_mui_2008R2_FR = "C:\Windows\winsxs\amd64_microsoft-windows-cleanmgr.resources_31bf3856ad364e35_6.1.7600.16385_fr-fr_5c4e3477a550d46a\cleanmgr.exe.mui"
# -Destination "C:\Windows\System32\fr-FR"
#---------------------------------------------------------------------------------------
# Windows Server 2008r2 64 bits en-US
$Path_exe_2008R2_US = "C:\Windows\winsxs\amd64_microsoft-windows-cleanmgr_31bf3856ad364e35_6.1.7600.16385_none_c9392808773cd7da\cleanmgr.exe"
# -Destination "C:\Windows\System32\"
$Path_mui_2008R2_US = "C:\Windows\winsxs\amd64_microsoft-windows-cleanmgr.resources_31bf3856ad364e35_6.1.7600.16385_en-us_b9cb6194b257cc63\cleanmgr.exe.mui"
# -Destination "C:\Windows\System32\en-US"
#----------------------------------------------------------------------------------------
# Windows Server 2012 64 bits fr-FR
$Path_exe_2012_FR = "C:\Windows\winsxs\x86_microsoft-windows-cleanmgr_31bf3856ad364e35_6.0.6001.18000_none_6d4436615d8bd133\cleanmgr.exe"
# -Destination "C:\Windows\System32\"
$Path_mui_2012_FR = "C:\Windows\winsxs\x86_microsoft-windows-cleanmgr.resources_31bf3856ad364e35_6.0.6001.18000_fr-fr_005942d08b9fcdc3\cleanmgr.exe.mui"
# -Destination "C:\Windows\System32\fr-FR"
$Path_mui_2012_US = "C:\Windows\winsxs\x86_microsoft-windows-cleanmgr.resources_31bf3856ad364e35_6.0.6001.18000_en-us_005942d08b9fcdc3\cleanmgr.exe.mui"
$VersionOS = [Version] (Get-WmiObject Win32_OperatingSystem).version
$VersionOSMajor = $VersionOS.major
$VersionOSMinor = $VersionOS.minor
#2008r2 - 6.1 - FR-FR & EN-en
IF ($VersionOSMinor -eq 1 -and $VersionOSMajor -eq 6){
#FR#
Copy-item $Path_exe_2008R2_FR -destination "C:\Windows\System32\"
Copy-item $Path_mui_2008R2_FR -destination "C:\Windows\System32\fr-FR"
#US#
Copy-item $Path_mui_2008R2_US -destination "C:\Windows\System32\en-US"
}
#2008 - 6.0 - FR-fr & EN-en
IF ($VersionOSMinor -eq 0 -and $VersionOSMajor -eq 6){
#FR#
Copy-item $Path_exe_2008_FR -destination "C:\Windows\System32\"
Copy-item $Path_mui_2008_FR -destination "C:\Windows\System32\fr-FR"
#US#
Copy-item $Path_mui_2008_US -destination "C:\Windows\System32\en-US"
}
#2012 - 6.2 - FR-fr & EN-en
IF ($VersionOSMinor -eq 2 -and $VersionOSMajor -eq 6){
#FR#
Copy-item $Path_exe_2012_FR -destination "C:\Windows\System32\"
Copy-item $Path_mui_2012_FR -destination "C:\Windows\System32\fr-FR"
#US#
Copy-item $Path_mui_2012_US -destination "C:\Windows\System32\en-US"
}
# 2012r2 - 6.3
IF ($VersionOSMinor -eq 3 -and $VersionOSMajor -eq 6){
Import-Module servermanager
Add-WindowsFeature Desktop-Experience -IncludeallSubFeature
}
#Mise en place des clefs de registre pour nettoyage automatique de toutes les fonctionnalitées suivantes :
$Flag = "StateFlags0200"
####
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\BranchCache" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\GameNewsFiles" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\GameStatisticsFiles" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\GameUpdateFiles" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Internet Cache Files" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Memory Dump Files" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Offline Pages Files" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Old ChkDsk Files" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Previous Installations" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Service Pack Cleanup" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Setup Log Files" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error memory dump files" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Setup Files" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Sync Files" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Thumbnail Cache" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Update Cleanup" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Upgrade Discarded Files" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\User file versions" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Defender" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Archive Files" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Queue Files" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Archive Files" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Queue Files" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows ESD installation files" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Upgrade Log Files" -Name $Flag -Value 2 -PropertyType DWORD -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders" -Name $Flag -Value 2 -PropertyType DWORD -Force
#Register de la tache planifiée et action et trigger
#2008r2 - 6.1 - FR-FR & EN-en
IF ($VersionOSMinor -eq 1 -and $VersionOSMajor -eq 6){
SCHTASKS /ru "SYSTEM" /RL HIGHEST /Create /SC DAILY /ST 05:00 /TN MYCORP_CleanMGR /TR "CleanMGR \SageRun:0200" /F
}
#2008 - 6.0 - FR-fr & EN-en
IF ($VersionOSMinor -eq 0 -and $VersionOSMajor -eq 6){
SCHTASKS /ru "SYSTEM" /RL HIGHEST /Create /SC DAILY /ST 05:00 /TN MYCORP_CleanMGR /TR "CleanMGR \SageRun:0200" /F
}
#2012 - 6.2
IF ($VersionOSMinor -eq 2 -and $VersionOSMajor -eq 6){
$Action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument 'CleanMGR \SageRun:0200'
$Trigger = New-ScheduledTaskTrigger -Daily -At 5am
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "PJ_CleanMGR" -Description "Full CleanMGR tous les jours a 5h du matin" -User "System" -Force
}
# 2012r2 - 6.3
IF ($VersionOSMinor -eq 3 -and $VersionOSMajor -eq 6){
$Action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument 'CleanMGR \SageRun:0200'
$Trigger = New-ScheduledTaskTrigger -Daily -At 5am
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "PJ_CleanMGR" -Description "Full CleanMGR tous les jours a 5h du matin" -User "System" -Force
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment