Skip to content

Instantly share code, notes, and snippets.

@markekraus
Forked from terrytrent/Servercleanup.ps1
Last active January 5, 2016 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markekraus/d71cba1cf9d2a1107891 to your computer and use it in GitHub Desktop.
Save markekraus/d71cba1cf9d2a1107891 to your computer and use it in GitHub Desktop.
$OS = (Get-WMIObject Win32_OperatingSystem).Caption
$DaysToDelete = 30
if(($OS -like "*2008*") -or ($OS -like "*2012*")){
$diskbefore = GWMI -Class win32_LogicalDisk -Filter "DeviceID = 'C:'" | Select-Object @{ n = "$OS"; e = {$_.PSComputerName}}, @{ n = "FreeSpace(GB) Before" ; e = {"{0:N1}" -f( $_.Freespace / 1gb ) } }, @{ n = "PercentFree Before" ; e = {"{0:P1}" -f( $_.FreeSpace / $_.Size ) } } | Out-String
Write-Output "$diskbefore"
}
else{
write-host @"
"##############################################################################"
"This script only supports Server 2008(R2) or 2012(R2), not $OS"
"############################################################################"
"@
break
}
switch -Wildcard ($OS){
"*2012*" {
dism.exe /online /Cleanup-Image /StartComponentCleanup
}
"*2008*" {
$2008Paths=@("amd64_microsoft-windows-cleanmgr_31bf3856ad364e35_6.1.7600.16385_none_c9392808773cd7da","amd64_microsoft-windows-cleanmgr.resources_31bf3856ad364e35_6.1.7600.16385_en-us_b9cb6194b257cc63")
$2008R2Paths=@("amd64_microsoft-windows-cleanmgr_31bf3856ad364e35_6.0.6001.18000_none_c962d1e515e94269","amd64_microsoft-windows-cleanmgr.resources_31bf3856ad364e35_6.0.6001.18000_en-us_b9f50b71510436f2")
$allPaths=@($2008Paths,$2008R2Paths)
$system32="$($env:SystemRoot)\System32\en-US"
$cleanmgrSystem32Paths=@("$system32\cleanmgr.exe","$system32\cleanmgr.exe.mui")
$existsAtLocation=$false
if((-Not (Test-Path -Path $cleanmgrSystem32Paths[0])) -or (-Not (Test-Path -Path $cleanmgrSystem32Paths[1]))){
foreach($path in $allPaths){
$cleanmgrPath="$($env:SystemRoot)\winsxs\$path[0]\cleanmgr.exe"
$cleanmgrmuiPath="$($env:SystemRoot)\winsxs\$path[1]\cleanmgr.exe.mui"
if(Test-Path -Path $cleanmgrPath){
Copy-Item -Path $cleanmgrPath,$cleanmgrmuiPath -Destination $system32 -Force -Confirm:$false
if($?){
$existsAtLocation=$true
}
else{
write-host " " -BackgroundColor Blue
write-host " Something happened " -ForegroundColor white -BackgroundColor Blue
write-host " " -BackgroundColor Blue
}
}
}
}
Else{
$existsAtLocation=$true
}
if(-Not ($existsAtLocation)){
"This $OS is not supported"
}
cleanmgr.exe /verylowdisk
}
}
$TempFolders = @()
$TempFolders += "C:\Windows\Temp\*"
$TempFolders += "C:\users\*\AppData\Local\Temp\*"
foreach($TempFolder in $TempFolders){
Get-ChildItem $TempFolder -Recurse -Force -ErrorAction SilentlyContinue | Where-Object { ($_.CreationTime -lt $(Get-Date).AddDays(-$DaysToDelete)) } | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
}
$diskafter = GWMI -Class win32_LogicalDisk -Filter "DeviceID = 'C:'" | Select-Object @{ n = "$OS"; e = {$_.PSComputerName}}, @{ n = "FreeSpace(GB) After" ; e = {"{0:N1}" -f( $_.Freespace / 1gb ) } }, @{ n = "PercentFree After" ; e = {"{0:P1}" -f( $_.FreeSpace / $_.Size ) } } | Out-String
Write-Output "$diskafter"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment