# Script to free up disk space on multiple computers # Last updated May 13 2021 # #Instructions: # # Place file 'computers.txt' into c:\temp folder with one PC name per line and run script as admin # script may take several hours to run with more than a few computers # It will write a result file in the same folder function ClearSCCMCache { #This will clear all folders from the c:\windows\ccmcache folder ## Initialize the CCM resource manager com object [__comobject]$CCMComObject = New-Object -ComObject 'UIResource.UIResourceMgr' ## Get the CacheElementIDs to delete $CacheInfo = $CCMComObject.GetCacheInfo().GetCacheElements() ## Remove cache items ForEach ($CacheItem in $CacheInfo) { $null = $CCMComObject.GetCacheInfo().DeleteCacheElement([string]$($CacheItem.CacheElementID)) } } # Start script # Write-Host "" Write-Host "Powershell script to clean disk space on a list of computers." Write-Host "Place txt file with computer names in c:\temp named computers.txt" Write-Host "On completion a report will be written to computers_report.txt." $SOURCEFILELOCATION = "c:\temp\computers.txt" If (!(Test-Path $SOURCEFILELOCATION)) { write-host "No computer list found. Exiting..." PAUSE EXIT} $computers = Get-Content c:\temp\computers.txt $number = 0 foreach ($comp in $computers){ # exit loop at end of file # if ( $comp -eq "" ) { break } $number = $number + 1 If ((Test-Connection -computer $comp -Quiet) -eq $false) {Write-Host "$comp is not pingable - continuing to next PC." Continue} If (!(Test-Path \\$comp\c$\'temp\')) {Write-Host "WinRM share on $comp is not available - continuing to next PC." Continue} Write-Host "" Write-Host "Working on computer " $number " : " $comp Write-Host "Current disk space is:" Get-CimInstance -ComputerName $comp win32_logicaldisk | where caption -eq "C:" | foreach-object {write " $($_.caption) $('{0:N2}' -f ($_.Size/1gb)) GB total, $('{0:N2}' -f ($_.FreeSpace/1gb)) GB free "} Write-Host "Cleaning Nomad cache..." Invoke-Command $comp {Start-Process -FilePath "C:\Program Files\1E\Client\Extensibility\NomadBranch\cachecleaner.exe" -ArgumentList "-deleteall", "-force=9" } -ErrorAction SilentlyContinue | Out-Null Write-Host "Cleaning unused Windows components..." invoke-command $comp {schtasks.exe /Run /TN "\Microsoft\Windows\Servicing\StartComponentCleanup"} | Out-Null Write-Host "Clearing SCCM cache..." invoke-command $comp -ScriptBlock ${Function:ClearSCCMCache} #Invoke-Command $comp {get-childitem -Directory -Path "c:\windows\ccmcache" |? {$_.psiscontainer -and $_.lastwritetime -le (get-date).adddays(-30)} |% {Remove-Item $_.fullname -force -Recurse } } invoke-command $comp {remove-item c:\windows\ccmcache\* -Recurse } Write-Host "Removing old user profiles. This can take a while..." Get-CimInstance win32_userprofile -computername $comp | Where {$_.LastUseTime -lt $(Get-Date).Date. AddDays(-$120) -and ($_.localpath -ne "c:\users\awhfy03") -and ($_.localpath -ne "c:\users\svc_bluescope_gscan")} | Remove-CimInstance Write-Host "Cleaning recycle bin..." Invoke-command $comp {Get-ChildItem -Path 'C:\$Recycle.Bin' -Force | Remove-Item -Recurse -ErrorAction SilentlyContinue } # check it again in a minute to see if commands have freed up some space... sleep 60 Write-Host "After cleanup disk space is now:" Get-CimInstance -ComputerName $comp win32_logicaldisk | where caption -eq "C:" | foreach-object {write " $($_.caption) $('{0:N2}' -f ($_.Size/1gb)) GB total, $('{0:N2}' -f ($_.FreeSpace/1gb)) GB free "} Write-Host "" } Write-Host "" Write-host "Finished processing computer list - EOF." Write-Host "" # sleep 60 # # Write a report to file with results # Write-Host "Creating a summary report in C:\temp..." foreach ($comp in $computers){ # exit loop at end of file # if ( $comp -eq "" ) { break } if ((Test-Connection -computer $comp -Quiet) -eq $false) {Write-Host "$comp is not pingable - continuing to next PC." Echo $comp | Out-File -FilePath c:\temp\computers_report.txt -Append -NoNewLine; Echo " is not pingable." | Out-File -FilePath c:\temp\computers_report.txt -Append Continue} If (!(Test-Path \\$comp\c$\'temp\')) {Write-Host "WinRM share on $comp is not available - continuing to next PC." Echo $comp | Out-File -FilePath c:\temp\computers_report.txt -Append -NoNewLine; Echo " does not have an accesible WinRM share." | Out-File -FilePath c:\temp\computers_report.txt -Append Continue} Try {Get-CimInstance -ComputerName $comp win32_logicaldisk -ErrorAction Stop | Out-Null } Catch { Echo $comp | Out-File -FilePath c:\temp\computers_report.txt -Append -NoNewLine; Echo " does not have an accesible WMI inventory." | Out-File -FilePath c:\temp\computers_report.txt -Append Continue} Echo $comp | Out-File -FilePath c:\temp\computers_report.txt -Append -NoNewLine ; Get-CimInstance -ComputerName $comp win32_logicaldisk | where caption -eq "C:" | foreach-object {write " $($_.caption) $('{0:N2}' -f ($_.Size/1gb)) GB total, $('{0:N2}' -f ($_.FreeSpace/1gb)) GB free "} -ErrorAction SilentlyContinue | Out-File -FilePath c:\temp\computers_report.txt -Append } Write-Host "Report completed and saved to c:\temp\computers_report.txt."