Here is a one-liner that returns the resolved name from the current user’s SID:
You may be arguing it is easier to query the environment variable $env:username for this, which is true. However, converting a SID to name can come handy in many scenarios. For example, if you must know the exact (localized) name for a well-known account or group, like the local Administrators group, simply use the language-neutral SID and have it translated:
PS> ([System.Security.Principal.SecurityIdentifier]'S-1-5-32-544').Translate( [System.Security.Principal.NTAccount]).Value
Likewise, the line below always lists local Administrators, regardless of which language your operating system uses:
$admins = ([System.Security.Principal.SecurityIdentifier]'S-1-5-32-544').Translate( [System.Security.Principal.NTAccount]).Value
$parts = $admins -split '\\'
$groupname = $parts[-1]
Get-LocalGroupMember -Group $groupname
psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU - with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!
ReTweet this Tip!