Repair damaged Documents and Desktop folder locations.
Run this as your normal user, in Windows PowerShell
Add-Type @" | |
using System; | |
using System.Runtime.InteropServices; | |
public class KNOWNFOLDERPATH { | |
/** https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetknownfolderpath */ | |
[DllImport("shell32.dll")] | |
private static extern int SHGetKnownFolderPath( | |
[MarshalAs(UnmanagedType.LPStruct)] | |
Guid rfid, | |
uint dwFlags, | |
IntPtr hToken, | |
out IntPtr pszPath | |
); | |
public static string ManagedSHGetKnownFolderPath(Guid rfid) { | |
IntPtr pszPath; | |
int hresult = SHGetKnownFolderPath(rfid, 0, IntPtr.Zero, out pszPath); | |
if (hresult != 0) { | |
// https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.externalexception | |
throw new ExternalException("-", hresult); | |
} | |
string path = Marshal.PtrToStringUni(pszPath); | |
// https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.freecotaskmem | |
Marshal.FreeCoTaskMem(pszPath); | |
return path; | |
} | |
/** https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shsetknownfolderpath */ | |
[DllImport("shell32.dll")] | |
public extern static int SHSetKnownFolderPath( | |
ref Guid folderId, | |
uint flags, | |
IntPtr token, | |
[MarshalAs(UnmanagedType.LPWStr)] string path | |
); | |
} | |
"@ | |
"%USERPROFILE%: $($env:USERPROFILE)" | |
# https://docs.microsoft.com/en-us/windows/win32/shell/knownfolderid | |
$KNOWNFOLDERIDs = @( | |
@{ | |
KNOWNFOLDERID = 'FOLDERID_Desktop' | |
GUID = 'B4BFCC3A-DB2C-424C-B029-7FE99A87C641' | |
DefaultPath = "$($env:USERPROFILE)\Desktop" | |
} | |
, @{ | |
KNOWNFOLDERID = 'FOLDERID_Documents' | |
GUID = 'FDD39AD0-238F-46AF-ADB4-6C85480369C7' | |
DefaultPath = "$($env:USERPROFILE)\Documents" | |
} | |
) | |
$KNOWNFOLDERIDs | ForEach-Object -Process { | |
$_ | |
[KNOWNFOLDERPATH]::ManagedSHGetKnownFolderPath($_.GUID) | |
[KNOWNFOLDERPATH]::SHSetKnownFolderPath([ref]$_.GUID, 0, 0, $_.DefaultPath) | |
} |