powershell-scripts/windows/Get-UserRegistryPath.ps1

16 lines
651 B
PowerShell

$Username = ((Get-CimInstance win32_computersystem | ForEach-Object username) -split '\\')[1]
Write-Host "Signed in user is '$Username'"
# Map PSDrive for HKU
New-PSDrive -Name HKU -PSProvider Registry -Root hkey_users | Out-Null
# Find matching Volatile Env
try {
$Environment = ((Get-ItemProperty 'HKU:\*\Volatile Environment' | Where-Object Username -eq $Username).PSPath -split '\\')[2]
$UserRegistryPath = Join-Path -Path 'HKU:' -ChildPath $Environment
} catch [System.Security.SecurityException] {
Write-Error "Permission Denied"
throw System.Security.SecurityException
}
Write-Host "User registry path is '$UserRegistryPath'"