C# finding all Microsoft Accounts on machine

234 views Asked by At

I am trying to get the UPN and Microsoft Accounts for all users on my machine using C#.

I am currently logged into an Azure account, and whoami returns details as expected and that I am looking for (but for all users):

PS C:\Windows\system32> whoami /upn
[email protected]
PS C:\Windows\system32> whoami
corp\my.name

The below C# code only returns users in Comuter Management mmc:

var searcher = new System.DirectoryServices.AccountManagement.PrincipalSearcher();
var userPrincipal = new UserPrincipal(new PrincipalContext(ContextType.Machine));
searcher.QueryFilter = userPrincipal;
var results = searcher.FindAll();

computer mangement

The powershell function Get-LocalUser | Select-Object Name,PrincipalSource seems not to provide more info either, despite saying it supports Microsoft Accounts:

Name               PrincipalSource
----               ---------------
Administrator                Local
DefaultAccount               Local
Guest                        Local
WDAGUtilityAccount           Local

As a final step to get the user by SID, by finding the SID profile @ Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList and using this with:

Get-localUser -SID S-1-12-1-1111111141-1234567891-3802628514-1659616000
Get-localUser : User S-1-12-1-1111111141-1234567891-3802628514-1659616000 was not found.

I have tried running as admin also and get the same output - pointers on how to get Microsoft Accounts with C# (or Powershell which i can then call via C#) much appreciated.

0

There are 0 answers