-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc
More file actions
47 lines (39 loc) · 1.39 KB
/
src
File metadata and controls
47 lines (39 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# STEP 1: Get the most recent uninstall event from the Event Log
$lastUninstallEvent = Get-WinEvent -LogName "Application" |
Where-Object {
$_.ProviderName -eq "MsiInstaller" -and
$_.Id -eq 1034
} |
Sort-Object TimeCreated -Descending |
Select-Object -First 1
if (-not $lastUninstallEvent) {
Write-Host "No recent uninstall event found."
exit
}
# STEP 2: Extract the product name from the event message
$productName = ($lastUninstallEvent.Message -split "`n") |
Where-Object { $_ -like "*Product Name*" } |
ForEach-Object { ($_ -split ":")[1].Trim() }
if (-not $productName) {
Write-Host "Failed to extract product name from event log."
exit
}
Write-Host "Last uninstalled software: $productName"
# STEP 3: Construct path to user's AppData\Local
$appDataLocal = Join-Path $env:LOCALAPPDATA ""
# STEP 4: Find matching folders
$matchingFolders = Get-ChildItem -Path $appDataLocal -Directory |
Where-Object { $_.Name -like "*$($productName)*" }
if ($matchingFolders.Count -eq 0) {
Write-Host "No folders in AppData\Local matched the product name."
exit
}
# STEP 5: Delete matching folders
foreach ($folder in $matchingFolders) {
try {
Remove-Item -Path $folder.FullName -Recurse -Force -ErrorAction Stop
Write-Host "Deleted: $($folder.FullName)"
} catch {
Write-Warning "Failed to delete: $($folder.FullName) - $_"
}
}