命令 line/Script 列出 C:\users 中的所有用户配置文件

Command line/Script to list all users profiles in C:\users

我正在尝试创建一个非常简单的脚本,它将显示 c:\users 中的所有用户配置文件,例如:

%User1%
%User2%

我试过将 WMIC 与循环一起使用,但它只是挂起....

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

如有任何帮助,我们将不胜感激。

试试这个批处理脚本:

@echo off
Call :WMICGET_ECHO UserAccount Name  " Name : "
Pause & Exit
::---------------------------------------------------------------------------------------------------------------------------------------
rem Based on this answer: 
: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
::---------------------------------------------------------------------------------------------------------------------------------------

以下示例与我在 中提供的示例不同,它应该仅列出 'normal' 个用户名,这些用户名的本地配置文件位置在 C:\Users.

'normal' 我的意思是它应该忽略像 'Default' 'Guest' 和 'Admin' 这样的帐户。

请注意,正如我在前面提到的评论中已经提到的,用户名对于当前在系统中的用户名是正确的,但不一定是他们的配置文件目录父名称中使用的名称。

@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

您可以删除脚本底部的 Pause 命令,如果您打算通过 CLI 运行 此批处理文件(而不是通过 GUI ).

您也可以直接在命令提示符中运行它:

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