$cs.UsrMgr_GetNameBySid
Description
Section titled “Description”Resolves a Windows SID to its account name, e.g. DOMAIN\username or BUILTIN\Administrators. Returns an empty string if the SID cannot be resolved to a name.
Syntax
Section titled “Syntax”$cs.UsrMgr_GetNameBySid(string SID)
Parameters
Section titled “Parameters”SID (String)
Section titled “SID (String)”The SID to resolve to an account name.
Return value
Section titled “Return value”[string] The resolved account name, or an empty string if the SID could not be resolved.
Example
Section titled “Example”$accountName = $cs.UsrMgr_GetNameBySid('S-1-5-21-3559337461-1037079836-4233830826-1109')if ($accountName) { $cs.Job_WriteLog("SID resolved to $accountName")}Resolve the currently logged-on user’s SID from their profile registry key to a friendly name for logging purposes:
$profileKey = Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList' | Where-Object { (Get-ItemProperty $_.PSPath).ProfileImagePath -like 'C:\Users\*' } | Select-Object -First 1
if ($profileKey) { $loggedOnSid = $profileKey.PSChildName $loggedOnName = $cs.UsrMgr_GetNameBySid($loggedOnSid) if ($loggedOnName) { $cs.Job_WriteLog("Installing for logged-on user: $loggedOnName") } else { $cs.Job_WriteLog("Could not resolve logged-on user SID $loggedOnSid") }}