Add Utility Scripts/Disk Cleanup.ps1
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
<#
|
||||
Author: Gabe Kerntke
|
||||
Date: 06/19/2025
|
||||
|
||||
.Synopsis
|
||||
Script cleans up disk space on the C: drive
|
||||
|
||||
.Reference
|
||||
|
||||
.Modified
|
||||
2025-06-19 (GabeK) - Script created
|
||||
#>
|
||||
|
||||
# Variables #
|
||||
$App_Name = "Windows Software Probe"
|
||||
$Size = Get-PSDrive C | Select-Object -ExpandProperty Free
|
||||
|
||||
# Functions #
|
||||
Function Remove-Probe_Cache {
|
||||
#Set file age to older than 60 days
|
||||
$DatetoDelete = (Get-Date).AddDays(-60)
|
||||
|
||||
#32 bit path for NablePatchCache
|
||||
$Path = "C:\Program Files\N-able Technologies\NablePatchCache"
|
||||
If ((Test-Path $Path) -eq $True) {
|
||||
Get-ChildItem -Path $Path -File | Where-Object { $_.LastWriteTime -lt $DatetoDelete -and $_.Name -notlike "*CacheMetaData*" } | Remove-Item -Confirm:$false -Force
|
||||
}
|
||||
|
||||
#64 bit path for NablePatchCache
|
||||
$Path = "C:\Program Files (x86)\N-able Technologies\NablePatchCache"
|
||||
If ((Test-Path $Path) -eq $True) {
|
||||
Get-ChildItem -Path $Path -File | Where-Object { $_.LastWriteTime -lt $DatetoDelete -and $_.Name -notlike "*CacheMetaData*" } | Remove-Item -Confirm:$false -Force
|
||||
}
|
||||
|
||||
#3rd party cache (UFS) - Recursive search for old files
|
||||
$Path = "C:\EPCache"
|
||||
If ((Test-Path $Path) -eq $True) {
|
||||
Get-ChildItem -Path $Path -File -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -Confirm:$false -Force
|
||||
}
|
||||
}
|
||||
|
||||
Function Remove-Agent_Cache {
|
||||
#Set File Age to older than 7 days
|
||||
$DatetoDelete = (Get-Date).AddDays(-7)
|
||||
|
||||
#32 Bit path for 3rd Party downloads
|
||||
$Path = "C:\Program Files\N-able Technologies\PatchManagement\ThirdPartyPatch\Downloads"
|
||||
If ((Test-Path $Path) -eq $True) {
|
||||
Get-ChildItem -Path $Path -File | Where-Object { $_.LastWriteTime -lt $DatetoDelete -and $_.Name -notlike "*.xml" } | Remove-Item -Confirm:$false -Force
|
||||
}
|
||||
|
||||
#64 Bit path for 3rd Party downloads
|
||||
$Path = "C:\Program Files (x86)\N-able Technologies\PatchManagement\ThirdPartyPatch\Downloads"
|
||||
If ((Test-Path $Path) -eq $True) {
|
||||
Get-ChildItem -Path $Path -File | Where-Object { $_.LastWriteTime -lt $DatetoDelete -and $_.Name -notlike "*.xml" } | Remove-Item -Confirm:$false -Force
|
||||
}
|
||||
|
||||
#New path for patch cache
|
||||
$Path = "C:\ProgramData\MspPlatform\FileCacheServiceAgent\cache"
|
||||
If ((Test-Path $Path) -eq $True) {
|
||||
Get-ChildItem -Path $Path -File | Where-Object { $_.LastWriteTime -lt $DatetoDelete -and $_.Name -notlike "*CacheMetaData*" } | Remove-Item -Confirm:$false -Force
|
||||
}
|
||||
|
||||
#Cleans out script cache
|
||||
$Path = "C:\Windows\NCentral\Cache"
|
||||
If ((Test-Path $Path) -eq $True) {
|
||||
Get-ChildItem -Path $Path -File | Where-Object { $_.LastWriteTime -lt $DatetoDelete -and $_.Name -notlike "*CacheMetaData*" } | Remove-Item -Confirm:$false -Force
|
||||
}
|
||||
}
|
||||
|
||||
Function Start-Disk_Cleanup {
|
||||
#Runs Disk Cleanup with all boxes checked and without user input
|
||||
CLEANMGR C:\ /VERYLOWDISK
|
||||
CLEANMGR C:\ /AUTOCLEAN
|
||||
}
|
||||
|
||||
Function Remove-Windows_Updates_Cache {
|
||||
#Stops services related to Windows Updates
|
||||
Stop-Service wuauserv
|
||||
Stop-Service BITS
|
||||
Start-Sleep 30
|
||||
|
||||
#Cleans out the Windows Updates patch cache folder
|
||||
Get-ChildItem "C:\Windows\SoftwareDistribution\Download\" | Remove-Item -Force -Recurse
|
||||
Start-Sleep 30
|
||||
|
||||
#Starts the services
|
||||
Start-Service wuauserv
|
||||
Start-Service BITS
|
||||
Start-Sleep 15
|
||||
}
|
||||
|
||||
#Check free space after running script
|
||||
Function Convert-Size {
|
||||
switch ($Size) {
|
||||
{ $_ -gt 1TB } { ($Size / 1TB).ToString("n2") + " TB"; break }
|
||||
{ $_ -gt 1GB } { ($Size / 1GB).ToString("n2") + " GB"; break }
|
||||
{ $_ -gt 1MB } { ($Size / 1MB).ToString("n2") + " MB"; break }
|
||||
{ $_ -gt 1KB } { ($Size / 1KB).ToString("n2") + " KB"; break }
|
||||
default { "$Size B" }
|
||||
}
|
||||
}
|
||||
|
||||
# Script Start #
|
||||
|
||||
If ((Test-Path "C:\Temp\Script Cache") -eq $False) { New-Item -ItemType directory -Path "C:\Temp\Script Cache" }
|
||||
try {
|
||||
Invoke-WebRequest -UseBasicParsing "https://scripts.gabesville.com/Gabesville/Powershell-Scripts/raw/branch/main/Functions%20Scripts/Functions.ps1" -outfile "C:\Temp\Script Cache\Functions.ps1"
|
||||
. "C:\Temp\Script Cache\Functions.ps1"
|
||||
}
|
||||
catch {
|
||||
#Use TLS 1.2 for communication
|
||||
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
||||
Invoke-WebRequest -UseBasicParsing "https://scripts.gabesville.com/Gabesville/Powershell-Scripts/raw/branch/main/Functions%20Scripts/Functions.ps1" -outfile "C:\Temp\Script Cache\Functions.ps1"
|
||||
. "C:\Temp\Script Cache\Functions.ps1"
|
||||
}
|
||||
|
||||
#Calculate starting free space
|
||||
[INT64]$StartFreeSpace = (Get-WmiObject -Class Win32_logicaldisk | Where-Object { $_.DeviceID -eq 'C:' }).Freespace
|
||||
|
||||
#Check to see if program is installed
|
||||
$Applist = Get-Software | Sort-Object -Descending
|
||||
|
||||
If ($Applist.DisplayName -like "*$App_Name*") {
|
||||
Remove-Probe_Cache
|
||||
}
|
||||
Remove-Agent_Cache
|
||||
Start-Disk_Cleanup
|
||||
Remove-Windows_Updates_Cache
|
||||
|
||||
#Calculate space freed up and report out
|
||||
[INT64]$EndFreeSpace = (Get-WmiObject -Class Win32_logicaldisk | Where { $_.DeviceID -eq 'C:' }).Freespace
|
||||
$SpaceFreed = "{0:N2}" -f (($EndFreeSpace - $StartFreeSpace) / 1GB)
|
||||
Write-Output "Space freed up: $($SpaceFreed)GB"
|
||||
$FreeAfter = Convert-Size
|
||||
Write-Host "$FreeAfter space free on C drive"
|
||||
# End Script #
|
||||
Reference in New Issue
Block a user