The following example, unlike that which I provided in my comment, should list only 'normal' UserNames, who have their local profile location within C:\Users
.
By 'normal' what I mean is that it should ignore accounts like 'Default' 'Guest' and 'Admin' for example.
Please note, as already mentioned in my fore-mentioned comment, the UserName will be correct for those currently in the system, but may not necessarily be the name used in their profile directory parent name.
@Echo Off & SetLocal EnableExtensions
For /F Tokens^=2^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe Path
Win32_UserProfile Where "Special!='TRUE' And LocalPath Like 'C:\\Users\\%%'"
Assoc:List 2^>NUL') Do For /F Tokens^=4^ Delims^=^" %%H In ('
%SystemRoot%\System32\wbem\WMIC.exe UserAccount Where "SID='%%G'" Assoc:List
/ResultRole:SID 2^>NUL') Do Echo %%H
Pause
You can remove the Pause
command at the bottom of the script, if you intend to run this batch file via the CLI, (as opposed to via the GUI).
You could also run it directly in the Command Prompt:
For /F Tokens^=2^ Delims^=^" %G In ('%SystemRoot%\System32\wbem\WMIC.exe Path Win32_UserProfile Where "Special!='TRUE' And LocalPath Like 'C:\\Users\\%'" Assoc:List 2^>NUL') Do @For /F Tokens^=4^ Delims^=^" %H In ('%SystemRoot%\System32\wbem\WMIC.exe UserAccount Where "SID='%G'" Assoc:List /ResultRole:SID 2^>NUL') Do @Echo %H