# Script to free up disk space on multiple computers # Place file 'computers.txt' into c:\temp folder with one PC name per line 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)) } } $computers = Get-Content c:\temp\computers.txt $number = 0 foreach ($comp in $computers){ $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 "Working on computer " $number " : " $comp Invoke-Command $comp {Start-Process -FilePath "C:\Program Files\1E\Client\Extensibility\NomadBranch\cachecleaner.exe" -ArgumentList "-deleteall", "-force=9" } -ErrorAction SilentlyContinue | Out-Null invoke-command $comp {schtasks.exe /Run /TN "\Microsoft\Windows\Servicing\StartComponentCleanup"} | Out-Null 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 } } 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 Invoke-command $comp {Get-ChildItem -Path 'C:\$Recycle.Bin' -Force | Remove-Item -Recurse -ErrorAction SilentlyContinue } 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 "} # check it again in a minute to see if commands have freed up space... sleep 60 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 "Finished processing computer list - EOF." # sleep 60 # # Write a report to file with results # foreach ($comp in $computers){ 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." # alternate methods to determine free space - might work better across Juniper than Get-CimInstance # $size = ([wmi]"\\remotecomputer\root\cimv2:Win32_logicalDisk.DeviceID='c:'").Size # $free = ([wmi]"\\remotecomputer\root\cimv2:Win32_logicalDisk.DeviceID='c:'").FreeSpace # Also you can divide the results by 1GB or 1MB if you want different units: # $disk = ([wmi]"\\remotecomputer\root\cimv2:Win32_logicalDisk.DeviceID='c:'") # "Remotecomputer C: has {0:#.0} GB free of {1:#.0} GB Total" -f ($disk.FreeSpace/1GB),($disk.Size/1GB) | write-output