<# FPS:Containers:DataResources:SC-DelTempFiles:ACTIVE #>

 

if (!(Push-NeededFor DelTempFiles)) {

    # Remove a prior patch if it already exists

    Remove-Item -Path Function:\DelTempFiles

} else {

    # Only load the help line if a prior release is not present, otherwise just keep whats there.

    $genShortcuts = @($genShortcuts

    (ConvertTo-DotFill $global:SCDotLen 'DelTempFiles')+"[] Removes temporary files from the user profile and cache items."

    )

}

 

function DelTempFiles {

 

<#

 

.DESCRIPTION

Module: DataResources

Shortcut: SC-DelTempFiles.ps1

Has Dependencies: NA

 

Get User Profiles and remove files and folder in the Temp Folder

Note: Ignores Access Errors if you do not have rights to access other user profiles

   

.EXAMPLE

if (Push-NeededFor SC-DelTempFiles) { . (Get-ActiveFileNm -PS1 SC-DelTempFiles) }

DelTempFiles

 

.CC

 

[2024.11.07 JD] Updates made to the shortcuts for ConvertTo-DotFill use

[2024.10.20 JD] Original code release

 

.NOTES

 

TODOs:

 

$tempPath = "D:\FPS\Media\Web"

# List all files and folders in the temporary folder, including hidden and system files

Get-ChildItem -Path $tempPath -Recurse -Force -Include *.~*

$tempPath = "C:\Users\jimdi\OneDrive\FPS\Media\Web"

# List all files and folders in the temporary folder, including hidden and system files

Get-ChildItem -Path $tempPath -Recurse -Force -Include *.~*

 

Then delete all of the files found. These may relate back to the temp files that were deleted

within the core of the script, but we will find out when we run CICD again.

 

#>

   

    [CmdletBinding()]

        param(

 

    )

 

    # Get the Path of the current user profile temp folder and create a String Format Pattern by replacing the username

    $tempPathArray = [environment]::GetEnvironmentVariable('TEMP').Split('\').Split('/')

    $tempPathArray[2] = '{0}'

    $tempPathPattern = $tempPathArray[2..9] -join '/'

 

    # Get list of all user profiles from Registry

    $ProfileList = (Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\')

    ForEach ($Profile in $ProfileList) {

        # Build User Profile Temp Folder prepending the path to the user profile folder

        $tempFolder = [System.IO.DirectoryInfo]::new(($tempPathPattern -f $Profile.GetValue('ProfileImagePath')))

        if (-not $tempFolder.Exists) {

            # Not a valid User Profile Temp Folder

            Continue

        }

 

        # Get list of all files and folder objects in Temp folder

        $PreList = @()

        $PreList = Get-ChildItem -Path $tempFolder.FullName -Force -Recurse -ErrorAction SilentlyContinue

 

        Write-Host ('Removing Files and Folders from: ({0})' -f $tempFolder.FullName)

        Write-Host ('Will be deleting: ({0}) File System Objects' -f $PreList.Count)

 

        If ($PreList) {

            $PreList.Where({$_.Exists}) | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue

            $PostList = Get-ChildItem -Path $tempFolder.FullName -Force -Recurse -ErrorAction SilentlyContinue

            Write-Host ('Skipped: ({0}) files' -f $PostList.Count)

        }

    }

 

}