# Live-hive shell-folder repair for a logged-in user whose Documents/Downloads
# sidebar is showing "this file has no associated app" after a folder-redirection
# GPO applies only the legacy (Personal) name but not the modern KnownFolder GUID.
#
# WHEN TO USE
#   The Folder Redirection CSE has written the UNC path to `Personal` / etc,
#   but the matching GUID value ({FDD39AD0-...} for Documents,
#   {374DE290-...} for Downloads) is still pointing at a local path, so
#   clicking the sidebar item tries to open the local folder and fails.
#
# HOW TO RUN
#   ScreenConnect Backstage PowerShell. User SHOULD be logged in so the hive
#   is live. Edit $SID and $UNCBase at the top before running.

[CmdletBinding()]
param(
    [Parameter(Mandatory = $true)]
    [string]$SID,

    [Parameter(Mandatory = $true)]
    [string]$UNCBase  # e.g. '\\CS-SERVER\homes\Sharon.Edwards'
)

$USF = "HKU\$SID\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
$SF  = "HKU\$SID\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"

Write-Host "=== BEFORE ==="
reg query "$USF" /v "Personal"                                   2>$null
reg query "$USF" /v "{FDD39AD0-238F-46AF-ADB4-6C85480369C7}"     2>$null
reg query "$USF" /v "{374DE290-123F-4565-9164-39C4925E467B}"     2>$null

$docsUNC = "$UNCBase\Documents"
$dlUNC   = "$UNCBase\Downloads"

# Documents - both legacy + GUID so the Explorer sidebar resolves cleanly
reg add "$USF" /v "Personal" /t REG_EXPAND_SZ /d $docsUNC /f | Out-Null
reg add "$USF" /v "{FDD39AD0-238F-46AF-ADB4-6C85480369C7}" /t REG_EXPAND_SZ /d $docsUNC /f | Out-Null
reg add "$SF"  /v "Personal" /t REG_SZ /d $docsUNC /f | Out-Null
reg add "$SF"  /v "My Documents" /t REG_SZ /d $docsUNC /f | Out-Null

# Downloads - modern GUID is the one the sidebar uses
reg add "$USF" /v "{374DE290-123F-4565-9164-39C4925E467B}" /t REG_EXPAND_SZ /d $dlUNC /f | Out-Null
reg add "$SF"  /v "{374DE290-123F-4565-9164-39C4925E467B}" /t REG_SZ /d $dlUNC /f | Out-Null

Write-Host "`n=== AFTER ==="
reg query "$USF" /v "Personal"
reg query "$USF" /v "{FDD39AD0-238F-46AF-ADB4-6C85480369C7}"
reg query "$USF" /v "{374DE290-123F-4565-9164-39C4925E467B}"

# Respawn Explorer for the logged-in user so the new values are picked up.
# Look up by SID rather than by username to avoid locale/spelling issues.
$user = (Get-CimInstance Win32_UserAccount | Where-Object { $_.SID -eq $SID }).Name
if ($user) {
    Write-Host "`n=== Restarting Explorer for $user ==="
    Get-Process explorer -IncludeUserName -ErrorAction SilentlyContinue |
        Where-Object { $_.UserName -like "*\$user" -or $_.UserName -like "$user" } |
        ForEach-Object { Write-Host "Killing PID $($_.Id) owner=$($_.UserName)"; Stop-Process -Id $_.Id -Force }
    Start-Sleep 3
    Get-Process explorer -IncludeUserName -ErrorAction SilentlyContinue |
        Where-Object { $_.UserName -like "*\$user" -or $_.UserName -like "$user" } |
        Select-Object Id, UserName, StartTime | Format-Table -AutoSize
} else {
    Write-Host "`n[WARN] Could not resolve SID to a username - user may need to sign out and back in for sidebar to refresh."
}
