-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFolderRedirectionCleanup.ps1
More file actions
59 lines (45 loc) · 2.13 KB
/
FolderRedirectionCleanup.ps1
File metadata and controls
59 lines (45 loc) · 2.13 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
48
49
50
51
52
53
54
55
56
57
58
59
# Run as Administrator
Write-Host "Starting Folder Redirection Cleanup..." -ForegroundColor Cyan
# 1. Reset User Shell Folders to default -- This needs to be ran as the user is logged in to ensure the necessary files are cleanedup
$defaultPaths = @{
"Desktop" = "%USERPROFILE%\Desktop"
"Personal" = "%USERPROFILE%\Documents"
"My Pictures" = "%USERPROFILE%\Pictures"
"My Music" = "%USERPROFILE%\Music"
"My Video" = "%USERPROFILE%\Videos"
"Favorites" = "%USERPROFILE%\Favorites"
"AppData" = "%USERPROFILE%\AppData\Roaming"
"Local AppData" = "%USERPROFILE%\AppData\Local"
"Cache" = "%USERPROFILE%\AppData\Local\Microsoft\Windows\INetCache"
}
$regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
foreach ($key in $defaultPaths.Keys) {
Set-ItemProperty -Path $regPath -Name $key -Value $defaultPaths[$key]
Write-Host "Reset $key to $($defaultPaths[$key])"
}
# 2. Delete Group Policy cache -- This needs to be ran as Admin to allow removal of previously cached GPOs
Remove-Item -Path "C:\Windows\System32\GroupPolicy" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -Path "C:\Windows\System32\GroupPolicyUsers" -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "Deleted Group Policy cache."
Write-Host "Disabling Offline Files using WMI..." -ForegroundColor Cyan
# Get the WMI object
$objWMI = [wmiclass]"\\.\root\cimv2:Win32_OfflineFilesCache"
# Disable Offline Files
$result = $objWMI.Enable($false)
# Check result
if ($result.ReturnValue -eq 0) {
Write-Host "Offline Files successfully disabled." -ForegroundColor Green
} else {
Write-Host "Failed to disable Offline Files. Error code: $($result.ReturnValue)" -ForegroundColor Red
}
# Reboot required?
if ($result.RebootRequired) {
Write-Host "A reboot is required to apply changes." -ForegroundColor Yellow
}
# 5. Force Group Policy update
gpupdate /force
Write-Host "Folder Redirection cleanup complete. Please reboot the workstation." -ForegroundColor Green
# 6. Reset Icon Cache DB
Remove-Item "$env:USERPROFILE\AppData\Local\IconCache.db" -Force
Stop-Process -Name explorer -Force
Start-Process explorer