Solution: That's the local machine SID. If you want the domain SID psgetsid pjrd5$ psgetsid. C:PSToolspsgetsid pjrd5,pjrd4,pjrd3.
I am looking for an easy way to get the SID for the current Windows user account. I know I can do it through WMI, but I don't want to go that route.
Apologies to everybody that answered in C# for not specifying it's C++. :-)
Franci Penov
Franci PenovFranci Penov67.1k1414 gold badges119119 silver badges156156 bronze badges
10 Answers
In Win32, call GetTokenInformation, passing a token handle and the
TokenUser
constant. It will fill in a TOKEN_USER structure for you. One of the elements in there is the user's SID. It's a BLOB (binary), but you can turn it into a string by using ConvertSidToStringSid.To get hold of the current token handle, use OpenThreadToken or OpenProcessToken.
If you prefer ATL, it has the CAccessToken class, which has all sorts of interesting things in it.
.NET has the Thread.CurrentPrinciple property, which returns an IPrincipal reference. You can get the SID:
Also in .NET, you can use WindowsIdentity.GetCurrent(), which returns the current user ID:
Roger LipscombeRoger Lipscombe57.7k4444 gold badges192192 silver badges319319 bronze badges
This should give you what you need:
using System.Security.Principal;
...
var sid = WindowsIdentity.GetCurrent().User;
The User property of WindowsIdentity returns the SID, per MSDN Docs
Jeffrey MeyerJeffrey Meyer2,81355 gold badges2424 silver badges2525 bronze badges
dex blackdex black
CodeProject has a few different methods you can try... You didn't mention what languages you wanted a solution in.
If you want to access it via a batch file or something, you can look as PsGetSid by Sysinternals. It translates SIDs to names and vice versa.
Kevin FairchildKevin Fairchild9,32255 gold badges2828 silver badges5050 bronze badges
In C# you can use either
using Microsoft.Win32.Security;
...
string username = Environment.UserName + '@' + Environment.GetEnvironmentVariable('USERDNSDOMAIN');
Sid sidUser = new Sid (username);
Or...
using System.Security.AccessControl;
using System.Security.Principal;
...
WindowsIdentity m_Self = WindowsIdentity.GetCurrent();
SecurityIdentifier m_SID = m_Self.Owner;');
silverbuggsilverbugg
cigar huangcigar huang
Ian BoydIan Boyd125k197197 gold badges705705 silver badges10221022 bronze badges
You didn't specify what language you want. But if you're up for C# this article offers both the WMI method as well as a faster (while more verbose) method utilizing the Win32 API.
I don't think there's currently another way of doing this without using WMI or the Win32 API.
Mel GreenMel Green2,48622 gold badges2121 silver badges3131 bronze badges
This one is the shortest of them all I believe.
Available with .net >= 3.5
nullpotentnullpotent8,46711 gold badge2626 silver badges4141 bronze badges
This question is tagged as
c++
And I answer in c++
language, So I recommend use of WMI tool :So, As WMI commands in
powershell
, bellow command get SID
of system-pc1
user :First, you need to get current
username
with bellow code
:Now you can try with
WQL
language and execute this query in c++
as bellow (in this example , I used of system-pc1
username in WQL_WIN32_USERACCOUNT_QUERY
query :This example does works properly!
BattleTestedBattleTested3,18022 gold badges1515 silver badges3737 bronze badges