. $([Scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/Tools.ps1')))

# When deleting temp files en masse, only delete files/folders older than the number of days specified here
$OlderThan = 7
$TimeDelta = New-TimeSpan -Days $OlderThan

# Remove all *.evtx files from system temporary files location
$EventLogs = Get-ChildItem -Path "${Env:SystemRoot}\TEMP\*" -File -Filter *.evtx
If ( $EventLogs ) {
    Write-Output "Deleting all event logs from ${Env:SystemRoot}\TEMP"
    ForEach ( $EventLog in $EventLogs ) { Remove-Item $EventLog -Force -ErrorAction SilentlyContinue }
}

# Remove misc logs and error reports
$MiscLogs = Get-ChildItem -Path "${Env:SystemRoot}\TEMP\*" -File -Include "${Env:COMPUTERNAME}-*.log", "AppxErrorReport_*.txt"
If ( $MiscLogs ) {
    Write-Output "Deleting misc logs from ${Env:SystemRoot}\TEMP"
    ForEach ( $MiscLog in $MiscLogs ) { Remove-Item $MiscLog -Force -ErrorAction SilentlyContinue }
}

# Remove archived CBS logs
$CBSLogs = Get-ChildItem -Path "${Env:SystemRoot}\Logs\CBS\*" -File -Include "CbsPersist_*.log", "CbsPersist_*.cab"
If ( $CBSLogs ) {
    $TotalBytes = 0
    ForEach ( $CBSLog in $CBSLogs) { $TotalBytes += $CBSLog.Length }
    $TotalSize = [math]::Round(($TotalBytes / 1MB),2)
    If ( $TotalSize -ge 200 ) {
        Write-Output "Total CBS log archive size: ${TotalSize}MB"
        Control-Service -Stop 'TrustedInstaller'
        Write-Output "Deleting primary CBS.log file"
        Remove-Item "${Env:SystemRoot}\Logs\CBS\CBS.log" -Force -ErrorAction SilentlyContinue
        Control-Service -Start 'TrustedInstaller'
    }
    Write-Output "Deleting archived CBS logs from ${Env:SystemRoot}\Logs\CBS"
    ForEach ( $CBSLog in $CBSLogs ) { Remove-Item $CBSLog -Force -ErrorAction SilentlyContinue }
}

# Remove all system temp files older than 7 days
$OldTempFiles = Get-ChildItem -Path "${Env:SystemRoot}\TEMP" | Where-Object { $_.LastWriteTime -lt ((Get-Date) - $TimeDelta) }
If ( $OldTempFiles ) {
    Write-Output "Deleting all temp files not modified in ${OlderThan} day(s) from ${Env:SystemRoot}\TEMP"
    ForEach ( $OldTempFile in $OldTempFiles ) { Remove-Item $OldTempFile.FullName -Force -Recurse -ErrorAction SilentlyContinue }
}

# Remove all old files from the Recycle Bin
$UserRecycleBins = Get-ChildItem -Path "${Env:SystemDrive}\`$Recycle.Bin" -Directory -ErrorAction SilentlyContinue
If ( ($UserRecycleBins | Measure-Object).Count -gt 0 ) {
    Write-Output "Deleting all recycle bin items older than ${OlderThan} days from:"
    ForEach ( $userRecycleBin in $UserRecycleBins ) {
        $userRecycleBinFullName = $userRecycleBin.FullName
        Write-Output "  - $userRecycleBinFullName"
        $items = Get-ChildItem -Path $userRecycleBin.FullName -Recurse -ErrorAction SilentlyContinue
        ForEach ( $item in $items ) {
            If ( $item.LastWriteTime -lt $TimeDelta ) {
                Try { Remove-Item -Path $item.FullName -Force -ErrorAction Continue }
                Catch { Write-Error $_.Exception.Message }
            }
        }
    }
}

# Turn off system hibernation if it's in use
If ( Test-Path "${Env:SystemDrive}\hiberfil.sys" ) {
    Write-Output "Turning off system hibernation"
    Try { Start-Process "powercfg" -ArgumentList "-h off" -Wait }
    Catch { Write-Error $_.Exception.Message }
}

# Remove PDCRevocation dump files
$PDCRevocationDumpFiles = Get-ChildItem -Path "${Env:SystemRoot}\LiveKernelReports\*" -File -Include "PDCRevocation-*.dmp"
If ( $PDCRevocationDumpFiles ) {
    Write-Output "Deleting PDCRevocation dump files from ${Env:SystemRoot}\LiveKernelReports"
    ForEach ( $PDCRevocationDump in $PDCRevocationDumpFiles ) { Remove-Item $PDCRevocationDump.FullName -Force -ErrorAction SilentlyContinue }
}

# Remove Genetec application crash dumps
If ( Test-Path "${Env:ProgramData}\Genetec\Dumps" ) {
    $GenetecCrashDumps = Get-ChildItem -Path "${Env:ProgramData}\Genetec\Dumps"
    If ( $GenetecCrashDumps )
    {
        Write-Output "Deleting Genetec application crash dumps"
        Try { ForEach ( $dump in $GenetecCrashDumps ) { Remove-Item $dump.FullName -Force -Recurse -ErrorAction SilentlyContinue } }
        Catch { Write-Error $_.Exception.Message }
    }
}

# Defragment Windows Elastic Search database
$ESDB = "${Env:ProgramData}\Microsoft\Search\Data\Applications\Windows\Windows.edb"
If ( Test-Path $ESDB ) {
    Try {
        Write-Output "Found Windows Elastic Search database: ""${ESDB}"""
        Write-Output "  - Disabling Windows Search service"
        Start-Process "sc.exe" -ArgumentList "config wsearch start= disabled" -Wait
        Write-Output "  - Stopping Windows Search Service"
        Start-Process "net.exe" -ArgumentList "stop wsearch" -Wait
        Write-Output "  - Defragmenting database file"
        Start-Process "esentutl.exe" -ArgumentList "/d ""${ESDB}""" -Wait
        Write-Output "  - Enabling Windows Search Service"
        Start-Process "sc.exe" -ArgumentList "config wsearch start= delayed-auto" -Wait
        Write-Output "  - Starting Windows Search Service"
        Start-Process "net.exe" -ArgumentList "start wsearch" -Wait
    }
    Catch { Write-Error $_.Exception.Message }

}

# Remove any Autodesk installers at their default location
$ADSKInst = "${Env:SystemDrive}\Autodesk"
If ( Test-Path $ADSKInst ) {
    $foldersToDelete = Get-ChildItem -Path $ADSKInst -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer) -and ($_.LastWriteTime -lt ((Get-Date) - $TimeDelta)) -and ( $_.Name -ne "WI") }
    ForEach ( $f in $foldersToDelete ) {
        $path = $f.FullName
        Write-Output "Deleting Autodesk installer directory: ""${path}"""
        Try { Remove-Item $f.FullName -Force -Recurse -ErrorAction SilentlyContinue }
        Catch { Write-Error $_.Exception.Message }
    }
}

# Delete all but most recent shadow copy of system drive
$SystemVolume = Get-CimInstance -ClassName Win32_Volume -ErrorAction SilentlyContinue | Where-Object { $_.Name -like "${Env:SystemDrive}\" }
$ShadowCopies = Get-CimInstance -ClassName Win32_ShadowCopy -ErrorAction SilentlyContinue | Where-Object { $_.VolumeName -like $SystemVolume.DeviceID }
If ( $ShadowCopies.Count -gt 1 ) {
    Write-Output "Deleting all of the oldest shadow copies of ${Env:SystemDrive}"
    $ShadowsToDelete = $ShadowCopies.Count - 1
    While ( $ShadowsToDelete -gt 0 ) {
        vssadmin delete shadows /For=$Env:SystemDrive /Oldest /Quiet
        $ShadowsToDelete = $ShadowsToDelete - 1
    }
}

# Delete old temp directories from Chocolatey cache
$ChocolateyCache = "${Env:WinDir}\Temp\chocolatey"
$ChocolateyTempDirs = Get-ChildItem -Path $ChocolateyCache -Directory -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -lt ((Get-Date) - $TimeDelta) }
If ( $ChocolateyTempDirs ) {
    Write-Output "Deleting all temp directories not modified in ${OlderThan} day(s) from ""${ChocolateyCache}"""
    ForEach ( $d in $ChocolateyTempDirs ) {
        Try { Remove-Item $d.FullName -Force -Recurse -ErrorAction SilentlyContinue }
        Catch { Write-Error $_.Exception.Message }
    }
}

# Remove files from user profile directories
ForEach ( $userProfile in $(Get-UserProfiles) ) {

    # Remove all user temp files older than 7 days
    $OldTempFiles = Get-ChildItem -Path "${userProfile}\AppData\Local\Temp" -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -lt ((Get-Date) - $TimeDelta) }
    If ( $OldTempFiles ) {
        Write-Output "Deleting all temp files not modified in ${OlderThan} day(s) from ""${userProfile}\AppData\Local\Temp"""
        ForEach ( $OldTempFile in $OldTempFiles ) { Remove-Item $OldTempFile.FullName -Force -Recurse -ErrorAction SilentlyContinue }
    }

    # Remove AutoCAD temp files
    If (-not (IsRunning -Name "acad") ) {
        $AutoCADTempFiles = Get-ChildItem -Path "${userProfile}\AppData\Local\Temp\*" -File -Include '*.ac$' -ErrorAction SilentlyContinue
        If ( $AutoCADTempFiles ) {
            Write-Output "Deleting AutoCAD temp files"
            ForEach ( $tempfile in $AutoCADTempFiles ) {
                Try { Remove-Item $tempfile.FullName -Force -ErrorAction SilentlyContinue }
                Catch { Write-Error $_.Exception.Message }
            }
        }
    }

    # Apple iTunes data
    # If (-not (IsRunning -Name 'itunes') ) {

    #     # Remove Apple software updates downloaded from iTunes
    #     $IPSWBasePath = "${userProfile}\AppData\Roaming\Apple Computer\iTunes"
    #     $IPSW = @()
    #     # iPhone update files
    #     ForEach ( $updateFile in $(Get-ChildItem -Path "${IPSWBasePath}\iPhone Software Updates\*" -File -Include '*.ipsw' -ErrorAction SilentlyContinue) ) {
    #         $IPSW += ,@($updateFile.FullName)
    #     }
    #     # iPad update files
    #     ForEach ( $updateFile in $(Get-ChildItem -Path "${IPSWBasePath}\iPad Software Updates\*" -File -Include '*.ipsw' -ErrorAction SilentlyContinue) ) {
    #         $IPSW += ,@($updateFile.FullName)
    #     }
    #     # iPod update files
    #     ForEach ( $updateFile in $(Get-ChildItem -Path "${IPSWBasePath}\iPod Software Updates\*" -File -Include '*.ipsw' -ErrorAction SilentlyContinue) ) {
    #         $IPSW += ,@($updateFile.FullName)
    #     }

    #     # Remove Apple mobile applications downloaded from iTunes
    #     $IPA = @()
    #     ForEach ( $mobileApp in $(Get-ChildItem -Path "${userProfile}\Music\iTunes\iTunes Media\Mobile Applications\*" -File -Include '*.ipa' -ErrorAction SilentlyContinue) ) {
    #         $IPA += ,@($updateFile.FullName)
    #     }
    
    #     If ( $IPSW ) {
    #         Write-Output "Found Apple software update files"
    #         ForEach ( $path in $IPSW ) {
    #             Write-Output "  - Deleting: ""${path}"""
    #             Try { Remove-Item -Path $path -Force -ErrorAction SilentlyContinue }
    #             Catch { Write-Error $_.Exception.Message }
    #         }
    #     }

    #     If ( $IPA ) {
    #         Write-Output "Found Apple mobile application files"
    #         ForEach ( $path in $IPA ) {
    #             Write-Output "  - Deleting: ""${path}"""
    #             Try { Remove-Item -Path $path -Force -ErrorAction SilentlyContinue }
    #             Catch { Write-Error $_.Exception.Message }
    #         }
    #     }
    # }
    
    # Remove old downloaded application files
    $OldDownloadedApps = (Get-ChildItem -Path "${userProfile}\Downloads\*" -File -Filter '*.exe' -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -lt ((Get-Date) - $TimeDelta) })
    If ( $OldDownloadedApps ) {
        Write-Output "Deleting old downloaded app files"
        ForEach ( $oldDownloadedApp in $OldDownloadedApps ) {
            $path = $oldDownloadedApp.FullName
            Write-Output "  - Deleting: ""${path}"""
            Try { Remove-Item -Path $path -Force -ErrorAction SilentlyContinue }
            Catch { Write-Error $_.Exception.Message }
        }
    }
    
    # Remove duplicate downloads
    # Separate the possible duplicates from the rest
    $PossibleDuplicates = @()
    $NotDuplicates = @()
    ForEach ( $file in (Get-ChildItem -Path "${userProfile}\Downloads\*" -File -ErrorAction SilentlyContinue) ) {
        If ( $file.BaseName -match '\s\(\d*\)$' ) {
            $PossibleDuplicates += ,@($file)
        } Else {
            $NotDuplicates += ,@($file)
        }
    }
    # Loop through all files
    ForEach ( $pd in $PossibleDuplicates ) {
        ForEach ( $nd in $NotDuplicates ) {
        
            # Match the first part of $pd to $nd
            If ( [regex]::Match($pd.BaseName,'^(.*)(\s\(\d*\))$').Groups[1].Value -eq $nd.BaseName ) {
            
                # Check for equal file size
                If ( $(Get-Item -Path $pd.FullName).Length -eq $(Get-Item -Path $nd.FullName).Length ) {
                
                    # Make sure the file still exists before writing any log messages
                    $ToDelete = $pd.FullName
                    If ( Test-Path $ToDelete ) {
                    
                        # Delete the duplicate file
                        Write-Output "Deleting duplicate download: ""${ToDelete}"""
                        Try { Remove-Item -Path $ToDelete -Force -ErrorAction SilentlyContinue }
                        Catch { Write-Error $_.Exception.Message }
                    }
                }
            }
        }
    }

    # Remove old Outlook backup files
    $OldOutlookBackupFiles = Get-ChildItem -Path "${userProfile}\AppData\Local\Microsoft\Outlook\*" -File -Filter '*.bak' -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -lt ((Get-Date) - $TimeDelta) }
    If ( $OldOutlookBackupFiles ) {
        Write-Output "Found Outlook backup files older than ${OlderThan} days:"
        ForEach ( $file in $OldOutlookBackupFiles ) {
            $path = $file.FullName
            Write-Output "Deleting: ""${path}"""
            Try { Remove-Item -Path $path -Force -ErrorAction SilentlyContinue }
            Catch { Write-Error $_.Exception.Message }
        }
    }
}

# Silently run cleanmgr.exe with default settings
# TODO: This does not run silently - commenting out for now!
#Try {
#    Write-Output "Running cleanmgr.exe with default settings for very low disk space"
#    Start-Process "cleanmgr.exe" -ArgumentList "/VERYLOWDISK"
#    Write-Output "  - Finished"
#}
#Catch { Write-Error $_.Exception.Message }