try { # Get the font file name $fontFile = "C:\\Users\\luctr\\AppData\\Roaming\\Captioneer\\fonts-cache\\Poppins-Black.ttf"; $fontFileName = [System.IO.Path]::GetFileName($fontFile); # Per-user fonts folder $userFontsFolder = [System.IO.Path]::Combine($env:LOCALAPPDATA, "Microsoft", "Windows", "Fonts"); if (-not (Test-Path $userFontsFolder)) { New-Item -ItemType Directory -Force -Path $userFontsFolder | Out-Null; } $userFontPath = [System.IO.Path]::Combine($userFontsFolder, $fontFileName); # If already present, exit successfully if (Test-Path $userFontPath) { Write-Output "Font already installed in user fonts folder: $userFontPath"; exit 0; } # Copy the font to the per-user fonts folder Copy-Item -Path $fontFile -Destination $userFontPath -Force; if (-not (Test-Path $userFontPath)) { Write-Output "Failed to install font"; exit 1; } # Register font in per-user registry (HKCU) $fontName = [System.IO.Path]::GetFileNameWithoutExtension($fontFileName); $ext = [System.IO.Path]::GetExtension($fontFileName).ToLowerInvariant(); if ($ext -eq ".otf") { $regSuffix = "(OpenType)" } elseif ($ext -eq ".ttc") { $regSuffix = "(TrueType)" } else { $regSuffix = "(TrueType)" } $regPath = "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"; New-Item -Path $regPath -Force | Out-Null; New-ItemProperty -Path $regPath -Name "$fontName $regSuffix" -Value $userFontPath -PropertyType String -Force | Out-Null; Write-Output "Font installed in user fonts folder: $userFontPath"; # Refresh font cache for current session try { Add-Type -TypeDefinition @' using System; using System.Runtime.InteropServices; public class FontCache { [DllImport("gdi32.dll")] public static extern int AddFontResource(string lpFilename); [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); } '@ [FontCache]::AddFontResource($userFontPath) | Out-Null; [FontCache]::SendMessage(0xffff, 0x001D, [IntPtr]::Zero, [IntPtr]::Zero) | Out-Null; Write-Output "Font cache refreshed"; } catch { Write-Output "Error refreshing font cache: $_"; } exit 0; } catch { Write-Error $_.Exception.Message; exit 1; }