Powershell script that will cleanup junk on following platforms: Server 2008R2, Server 2012R2, Server 2016

You can find required files for Server 2008R2 and Server 2012R2 on the following DropBox link: https://www.dropbox.com/s/981nmo1bey2c6c9/CleanMgr.rar?dl=0

Create shared folder on the network (DFS for example), as provided by example in script, define paths to folders for server versions in script.



1. All files and folders that are not used by system processes from system temp folder

2. All files and folders for each user profile on the system from temp folder

3. All files and folders from system recycle bin

At the end, it will run disk cleanup tool and erase all of the files that we left behind.


# Define path to disk cleanup tool - cleanmgr 
$Srv2012Programpath = "\\path.to\sharedfolder" # For Example "\\domain.name\IT\tools\CleanMgr\Srv2012R2" 
$Srv2008Programpath = "\\path.to\sharedfolder" # For Example "\\domain.name\IT\tools\CleanMgr\Srv2008R2" 
# List all of the servers in AD forest 
$Serverlist = Get-ADComputer -Filter {OperatingSystem -like "*server*" -and Enabled -eq $True-Properties * | select Name,OperatingSystem 
# Copy cleanmgr to server distributions 
foreach ($Server in $Serverlist) { 
    if ($($Server.OperatingSystem) -like "*Server 2012 R2*") { 
    $Checkprogram = Test-Path -Path "\\$($Server.Name)\C$\Windows\System32\cleanmgr.exe" 
        if ($Checkprogram -eq $false) { 
            Copy-Item -Path "$Srv2012Programpath\cleanmgr.exe" -Destination "\\$($Server.Name)\C$\Windows\System32\" 
            Copy-Item -Path "$Srv2012Programpath\cleanmgr.exe.mui" -Destination "\\$($Server.Name)\C$\Windows\System32\en-us\" 
    elseif ($($server.operatingsystem) -like "*Server 2008*") { 
    $Checkprogram = Test-Path -Path "\\$($Server.Name)\C$\Windows\System32\cleanmgr.exe" 
        if ($Checkprogram -eq $false) { 
            Copy-Item -Path "$Srv2008Programpath\cleanmgr.exe" -Destination "\\$($Server.Name)\C$\Windows\System32\" 
            Copy-Item -Path "$Srv2008Programpath\cleanmgr.exe.mui" -Destination "\\$($Server.Name)\C$\Windows\System32\en-us\" 
# Invoke cleanup actions on servers after delivering of cleanmgr program 
foreach ($Server in $Serverlist) { 
    Invoke-Command -ComputerName $($Server.Name) -ScriptBlock { 
    # Get all user names from C 
    $Users = (Get-ChildItem C:\users).Name 
    # Define recycle bin object 
    $objShell = New-Object -ComObject Shell.Application 
    $objFolder = $objShell.NameSpace(0xA) 
    # Empty recycle bin 
    $objFolder.Items() | %{Remove-Item $_.path -Confirm:$false -Recurse -Force} 
    # Clean junk files with disk cleanup 
    cleanmgr.exe /sagerun:1 
    # Loop temp folder removal for all found users 
    foreach ($User in $Users) { 
        # Remove junk from user temp folder 
        Remove-Item -Path "C:\Users\$User\Appdata\Local\Temp" -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue 
        # Remove junk from Windows temp folder 
        Remove-Item -Path "C:\Windows\Temp" -Recurse -Force -Confirm:$False -ErrorAction SilentlyContinue