1

I am trying to create a very simple script that will display all the User profiles in c:\users like:

%User1%
%User2%

I have tried using WMIC with a loop and it just hangs....

for /f "tokens=* skip=1" %%a in ('wmic UserAccount get Name') do (
    if not "%%a"=="" (
       
    )
)

Any help would be appreciated.

Compo
  • 36,585
  • 5
  • 27
  • 39
doheth
  • 87
  • 9
  • 2
    User profiles are not always in `C:\Users`, so the simplest way to get those is `For /D %%G In ("C:\Users\*") Do @Echo %%~nxG`. However that will return the original name used when the user account was created, not necessarily whatever their chosen user name is now. Can you please [edit] your question, to clarify exactly what you're trying to achieve? – Compo Feb 26 '21 at 19:25

3 Answers3

2

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
Compo
  • 36,585
  • 5
  • 27
  • 39
0

Give a try for this batch script :

@echo off
Call :WMICGET_ECHO UserAccount Name  " Name : "
Pause & Exit
::---------------------------------------------------------------------------------------------------------------------------------------
rem Based on this answer: https://stackoverflow.com/a/63153964/8262102
:WMICGET_ECHO
@for /f "skip=1 delims=" %%a in ('"wmic %1 get %2"') do (@for /f "delims=" %%b in ("%%a") do echo %~3 %%~nb & set "WMICGET_VALUE=%%~nb")
goto :EOF
::---------------------------------------------------------------------------------------------------------------------------------------
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • 1
    Just be aware that this lists all user names, not those which are located in `C:\Users`, and also includes some which aren't really what you'd call 'normal' users. – Compo Feb 26 '21 at 22:20
0
dir /B "%systemdrive%\users" | findstr /l /v Public
  • While providing a code-only answer may answer the question, it’s recommended to provide an explanation for any additions or edits made. That way, future readers can understand your solution. Please [edit] your answer to add context and/or an explanation to your code. – Fastnlight Nov 02 '22 at 20:46