-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcore.ps1
255 lines (214 loc) · 8 KB
/
core.ps1
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
$paths = @{
RoamingFolder = Join-Path $env:APPDATA "Spotify"
LocalFolder = Join-Path $env:LOCALAPPDATA "Spotify"
RoamingFolderSpice = Join-Path $env:APPDATA "spicetify"
LocalFolderSpice = Join-Path $env:LOCALAPPDATA "spicetify"
UninstallExe = Join-Path ([System.IO.Path]::GetTempPath()) "SpotifyUninstall.exe"
TempSearch = Join-Path ([System.IO.Path]::GetTempPath()) "SpotX_Temp*"
DesktopShortcut = Join-Path $env:USERPROFILE "Desktop\Spotify.lnk"
StartMenuShortcut = Join-Path $env:APPDATA "Microsoft\Windows\Start Menu\Programs\Spotify.lnk"
}
$registryKeys = @(
"HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\Spotify"
"HKCU:\Software\Spotify"
"HKCU:\Software\Classes\spotify"
"HKCU:\Software\Microsoft\Internet Explorer\Low Rights\ElevationPolicy\{5C0D11B8-C5F6-4be3-AD2C-2B1A3EB94AB6}"
"HKCU:\Software\Microsoft\Internet Explorer\Low Rights\DragDrop\{5C0D11B8-C5F6-4be3-AD2C-2B1A3EB94AB6}"
)
$registryValue = @(
@{
Path = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run"
Name = "Spotify Web Helper"
}
@{
Path = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run"
Name = "Spotify"
}
)
$removedItems = 0
$errorMessages = @()
$ieCachePath = Join-Path $env:LOCALAPPDATA "Microsoft\Windows\INetCache\IE"
function Find-ItemsToRemove {
$foundItems = @{
FilesFolders = @()
RegistryKeys = @()
RegistryValues = @()
StoreApp = $null
TempSearchFiles = @()
IeCacheFiles = @()
}
$sortedItems = $paths.GetEnumerator() | Where-Object { $_.Key -ne "TempSearch" } | ForEach-Object {
if (Test-Path $_.Value) {
Get-Item $_.Value
}
} | Sort-Object @{Expression = { -not $_.PSIsContainer } }, @{Expression = { $_.Extension -ne ".lnk" } }
$foundItems.FilesFolders = $sortedItems.FullName
$foundItems.TempSearchFiles = @(Get-ChildItem -Path $paths.TempSearch -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName)
$foundItems.IeCacheFiles = @(Get-ChildItem -Path $ieCachePath -Force -Recurse -Filter "SpotifyFullSetup*" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName)
$registryKeys | ForEach-Object {
if (Test-Path $_) { $foundItems.RegistryKeys += $_ }
}
$registryValue | ForEach-Object {
if (Get-ItemProperty -Path $_.Path -Name $_.Name -ErrorAction SilentlyContinue) {
$foundItems.RegistryValues += $_
}
}
$foundItems.StoreApp = Get-AppxPackage -Name "SpotifyAB.SpotifyMusic" -ErrorAction SilentlyContinue
return $foundItems
}
function Remove-ItemSafely {
param([string]$Path)
if (Test-Path -LiteralPath $Path) {
try {
Remove-Item -LiteralPath $Path -Recurse -Force -ErrorAction Stop
Write-Host "Removed: $Path"
return 1
}
catch {
$script:errorMessages += "Failed to remove $Path : $($_.Exception.Message)"
return 0
}
}
return 0
}
function Remove-FoundItems {
param($foundItems)
$count = 0
if ($foundItems.StoreApp) {
try {
$ProgressPreference = 'SilentlyContinue'
Remove-AppxPackage -Package $foundItems.StoreApp.PackageFullName -ErrorAction Stop
Write-Host "Removed: Spotify Store version"
$count++
}
catch {
$script:errorMessages += "Failed to remove Store app: $_"
}
}
# Remove files/folders
$foundItems.FilesFolders + $foundItems.TempSearchFiles + $foundItems.IeCacheFiles | ForEach-Object {
$count += Remove-ItemSafely -Path $_
}
# Remove registry keys
$foundItems.RegistryKeys | ForEach-Object {
try {
Remove-Item -Path $_ -Recurse -Force -ErrorAction Stop
Write-Host "Removed: $_"
$count++
}
catch {
$script:errorMessages += "Failed to remove registry key $_ : $_"
}
}
# Remove registry values
$foundItems.RegistryValues | ForEach-Object {
try {
Remove-ItemProperty -Path $_.Path -Name $_.Name -Force -ErrorAction Stop
Write-Host "Removed: $($_.Path)\value=$($_.Name)"
$count++
}
catch {
$script:errorMessages += "Failed to remove registry value $($_.Name) : $_"
}
}
return $count
}
function Get-RemovalSummary {
if ($errorMessages.Count -gt 0 -or $removedItems -gt 0) {
if ($removedItems -gt 0) {
Write-Host "`nCleanup completed: removed $removedItems items"
}
if ($errorMessages.Count -gt 0) {
Write-Host "`nErrors encountered ($($errorMessages.Count)):"
$errorMessages | ForEach-Object { Write-Warning $_ }
Write-Host "`nSome items may require manual removal."
}
}
else {
Write-Host "No traces of Spotify were detected"
}
if ($Host.Name -eq "ConsoleHost") {
Write-Host "`nPress any key to exit..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
}
function Stop-SpotifyProcesses {
param(
[int]$maxAttempts = 5,
[int]$retryDelay = 1000
)
for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) {
$processes = Get-Process -Name "Spotify" -ErrorAction SilentlyContinue
if (-not $processes) { break }
$processes | ForEach-Object {
try {
Stop-Process -Id $_.Id -Force -ErrorAction Stop
}
catch {
$script:errorMessages += "Failed to stop process $($_.Name) (PID: $($_.Id)): $_"
}
}
Start-Sleep -Milliseconds $retryDelay
}
}
function Reset-TargetACLs {
param(
[Parameter(Mandatory)]
$foundItems
)
$aclPaths = $foundItems.FilesFolders + $foundItems.TempSearchFiles + $foundItems.IeCacheFiles
foreach ($path in $aclPaths) {
if (Test-Path -Path $path) {
try {
Write-Verbose "Resetting ACLs for: $path"
$result = icacls $path /reset /T /Q
if ($LASTEXITCODE -ne 0) {
throw "icacls failed with exit code $LASTEXITCODE"
}
}
catch {
Write-Warning "Failed to reset ACLs for $path : $_"
}
}
}
}
try {
# Stop all running Spotify processes before cleanup
Stop-SpotifyProcesses -retryDelay 500
if ($PSVersionTable.PSVersion.Major -ge 7) {
Import-Module Appx -UseWindowsPowerShell -WarningAction SilentlyContinue
}
Write-Host "Search items..."
Write-Host
$itemsToRemove = Find-ItemsToRemove
$maxAttempts = 5
$attempt = 0
do {
$attempt++
# Reset access control lists (ACLs) to ensure proper file/folder access
Reset-TargetACLs -foundItems $itemsToRemove
# Remove all identified Spotify-related items (files, registry entries, etc.)
$removedItems += Remove-FoundItems -foundItems $itemsToRemove
# Scan again for any remaining items that need cleanup
$itemsToRemove = Find-ItemsToRemove
# Exit loop if maximum attempts reached to prevent infinite loops
if ($attempt -ge $maxAttempts) {
Write-Host "The maximum number of attempts $($maxAttempts) has been reached. Terminating the loop"
break
}
if ($attempt -ge 1) { Start-Sleep -Milliseconds 1500 }
} while (($itemsToRemove.FilesFolders.Count +
$itemsToRemove.RegistryKeys.Count +
$itemsToRemove.RegistryValues.Count +
$itemsToRemove.TempSearchFiles.Count +
$itemsToRemove.IeCacheFiles.Count) -gt 0 -or
$itemsToRemove.StoreApp)
# Display final cleanup summary and any encountered errors
Get-RemovalSummary
}
catch {
Write-Host "Critical error:" -ForegroundColor Red -NoNewline
Write-Host " $($_.Exception.Message)"
Write-Host "$($_.ScriptStackTrace)"
exit 1
}