Remove-TempFiles

Have you ever had a user call in and say that they have run out of disk space? We tend to see that when computers are deployed with small hard drives. Here is a function that you may find handy to use for these issues. You can run it as a job by removing the comment #-Jobs. Remove Comment for Get-Job to get the job status.



 <#
.Synopsis
   Remove-TempFiles v1.0
   By Michael J. Thomas
   Created 06/03/2019
   Updated 06/03/2019
    
.DESCRIPTION
   Removes the Temp Files on local and remote computers. It also disables hibernation to save space. 
   C:\temp\, c:\windows\temp\, c:\windows\prefetch\, and all Users Appdata\local\Temp\ files. 
.EXAMPLE
   Remove the Temp files on the local computer.
   Remove-TempFiles
.EXAMPLE
    Remove the Temp files on a remote computer.
    Remove-TempFiles -ComputerName "Computer01"
.EXAMPLE
    Remove the Temp files on remote computers.
    Remove-TempFiles -UserName "Computer01","Computer02"
#>

Function Remove-TempFiles{
Param(
[string[]]$ComputerName = $env:COMPUTERNAME
)

Foreach ($Computer in $ComputerName){
Invoke-Command -ComputerName $ComputerName -ScriptBlock{
powercfg /hibernate off
Start-Sleep 2
Remove-Item "C:\Windows\ccmcache\*", "C:\temp\*","c:\windows\temp\*",“C:\Windows\Prefetch\*”,“C:\Users\*\Appdata\Local\Temp\*” -Force -Recurse -Verbose
} #-AsJob -JobName "CleanMgrJob"
#Get-Job -Name "CleanMgrJob"
}
}

Comments

Popular posts from this blog

Add-RemoteDesktopUsers

Invoke-Reprofile

Keep All Network Connections Private ( Set-PrivateNetwork )