In some scripts, I've found it useful to be able to obtain a user's SID and, conversely, obtain the username associated with a SID. To perform these tasks, I created the GetSIDFromUser and GetUserFromSID functions. The GetSIDFromUser function uses Windows Management Instrumentation's (WMI's) Win32_UserAccount class to retrieve an account's SID. The GetUserFromSID function uses WMI's Win32_SID class to return the account name for a SID.
GetSIDFromUser
Listing 1 shows the VBScript version of GetSIDFromUser. You call the function with the code
GetSIDFromUser(username)
where username is the name of the user whose SID you want. The username parameter can include a domain name (e.g., domain\username). If you don't specify a domain name, the current logon domain is assumed.
The function first determines whether the passed parameter contains a backslash (\). If it does, the function separates the passed parameter into a domain name and username. The fully qualified WMI path requires both parts, so if no backslash is present, the WScript.Network object's UserDomain property value is used for the domain name. . . .