$env:USERDOMAIN is not your identity
env paths · case
Symptom
Section titled “Symptom”An ACL/permissions script builds a principal as "$env:USERDOMAIN\$env:USERNAME"
and passes it to icacls. It works on the dev machine, then fails on a renamed
computer, a Microsoft-account login, or an AzureAD-joined machine: icacls cannot
resolve the principal, the grant fails, and if the code fails closed, the whole
feature dies.
# On a workgroup (non-domain) machine:$env:USERDOMAIN # -> COMPUTERNAME, not a domain# Microsoft-account login: local profile name != account nameicacls secret.txt /grant "$env:USERDOMAIN\$env:USERNAME:(R)"# -> "No mapping between account names and security IDs was done."USERDOMAIN is always set — on a workgroup machine it silently holds the computer
name, so domain ? "domain\user" : user fallbacks never fire. Both variables are
also plain environment values, writable by whatever launched the process, which
makes them attacker-influenceable in a permissions path.
Workaround
Section titled “Workaround”Use the SID, which is what the token actually carries and which icacls accepts directly:
$sid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Valueicacls secret.txt /grant "*${sid}:(R)"The referenced production fix replaced the env-derived principal with the current token’s SID for exactly the failure modes above.