Public/Application/Search-AppData.ps1
<#
Copyright © 2024 Integris. For internal company use only. All rights reserved. #> FUNCTION Search-AppData { <# .SYNOPSIS Searches for folders in the AppData directories based on specified keywords and performs actions on them. .DESCRIPTION This function searches for folders in the AppData and LocalAppData directories that match specified keywords. It can optionally rename or delete the matched folders. .PARAMETER Keyword Specifies the keywords to search for in folder names. .PARAMETER ExactMatch Indicates whether to search for exact matches of the keywords. .PARAMETER Rename Indicates whether to rename the matched folders. .PARAMETER Delete Indicates whether to delete the matched folders. .EXAMPLE Search-AppData -Keyword "example" -Rename Searches for folders with "example" in their names and renames them. .NOTES The function searches the AppData and LocalAppData directories and performs the specified actions on matched folders. #> [CmdletBinding(DefaultParameterSetName='None')] param( [Parameter(Mandatory)] [Parameter(ParameterSetName = 'None')] [Parameter(ParameterSetName = 'Rename')] [Parameter(ParameterSetName = 'Delete')] [string[]]$Keyword, [Parameter(ParameterSetName = 'None')] [Parameter(ParameterSetName = 'Rename')] [Parameter(ParameterSetName = 'Delete')] [switch]$ExactMatch = $False, [Parameter(ParameterSetName = 'Rename')] [switch]$Rename = $False, [Parameter(ParameterSetName = 'Delete')] [switch]$Delete = $False ) $ActionTaken = "None" IF ($Rename -eq $True) { $ActionTaken = "Rename" } IF ($Delete -eq $True) { $ActionTaken = "Delete" } $Folders = Get-ChildItem -Path $env:APPDATA,$env:LOCALAPPDATA -Directory $Results = @() FOREACH ($Folder in $Folders) { IF ($Folder.Name -like "*_bak_*") { continue } FOREACH ($Item in $Keyword) { IF ($ExactMatch -eq $False) { IF ($Folder -Like "*$Item*") { IF ($Delete -eq $True) { TRY { Remove-Item -Path $Folder.FullName -Force -ErrorAction SilentlyContinue $ActionResult = "Successful" IF ($null -ne (Get-Item -Path $Folder.FullName -ErrorAction SilentlyContinue)) { $ActionResult = "Failed" } } CATCH { IF ($null -ne (Get-Item -Path $Folder.FullName)) { $ActionResult = "Failed" } ELSE { $ActionResult = "Successful" } } } ELSEIF ($Rename -eq $True) { $Now = Get-Date $NewName = $Folder.FullName $NewName += "_bak_$($Now.ToString('yyy_MM_dd_HH_mm_ss'))" TRY { Rename-Item -Path $Folder.FullName -NewName $NewName -Force -ErrorAction SilentlyContinue $ActionResult = "Successful" IF ($null -ne (Get-Item -Path $Folder.FullName -ErrorAction SilentlyContinue)) { $ActionResult = "Failed" } } CATCH { IF ($null -ne (Get-Item -Path $Folder.FullName -ErrorAction SilentlyContinue)) { $ActionResult = "Failed" } ELSE { $ActionResult = "Successful" } } } ELSE { $ActionResult = "N/A" } $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{ FolderName = $Folder.Name FolderPath = $Folder.FullName RequestedAction = $ActionTaken ActionResult = $ActionResult } break } } ELSEIF ($ExactMatch -eq $True) { IF ($Folder -Like "$Item") { IF ($Delete -eq $True) { TRY { Remove-Item -Path $Folder.FullName -Force -ErrorAction SilentlyContinue $ActionResult = "Successful" IF ($null -ne (Get-Item -Path $Folder.FullName -ErrorAction SilentlyContinue)) { $ActionResult = "Failed" } } CATCH { IF ($null -ne (Get-Item -Path $Folder.FullName -ErrorAction SilentlyContinue)) { $ActionResult = "Failed" } ELSE { $ActionResult = "Successful" } } } ELSEIF ($Rename -eq $True) { $Now = Get-Date $NewName = $Folder.FullName $NewName += "_bak_$($Now.ToString('yyy_MM_dd_HH_mm_ss'))" TRY { Rename-Item -Path $Folder.FullName -NewName $NewName -Force -ErrorAction SilentlyContinue $ActionResult = "Successful" IF ($null -ne (Get-Item -Path $Folder.FullName -ErrorAction SilentlyContinue)) { $ActionResult = "Failed" } } CATCH { IF ($null -ne (Get-Item -Path $Folder.FullName -ErrorAction SilentlyContinue)) { $ActionResult = "Failed" } ELSE { $ActionResult = "Successful" } } } ELSE { $ActionResult = "N/A" } $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{ FolderName = $Folder.Name FolderPath = $Folder.FullName RequestedAction = $ActionTaken ActionResult = $ActionResult } break } } } } FOREACH ($Result in $Results) { IF ($Result.ActionResult -eq "Failed") { Write-Host "" Write-Warning "$ActionTaken Failed: Verifiy the Associated Application is Not Running" } } RETURN $Results | Select-Object FolderName, FolderPath, RequestedAction, ActionResult } |