我正在使用以下批处理命令来检索所有本地用户配置文件(也包括域用户):
for /f "delims=" %%I in ('dir /a:d-h /b "%SystemDrive%\Users\*" 2^>nul ^| %SystemRoot%\System32\findstr.exe /i /l /x /v /g:"%bin%\exclude_users.txt"') do (
问题在于该命令有其局限性:它不会真正检查所涉及的用户是否确实拥有一个帐户。
用户Compo为我提供了一种使用WMIC检索配置文件名称的方法。
因此,我最终编写了以下命令:
@For /F "tokens=* skip=1" %%I In ('%__AppDir__%wbem\WMIC.exe UserAccount Get Name ^|%__AppDir__%findstr.exe /i /l /x /v /g:"%bin%\exclude_users.txt"') do (
问题是:它忽略了我的排除文件(每行包含一个用户),并且最终生成了一个没有任何名称的配置文件。
知道如何解决这些问题吗?
最佳答案
@echo off
setlocal
set "bin=%~dp0"
for /f "tokens=* skip=1" %%I in ('
%__AppDir__%wbem\WMIC.exe UserAccount where Disabled^="FALSE" get Name ^|
%__AppDir__%WindowsPowerShell\v1.0\powershell -noprofile -command "$input.trim()" ^|
%__AppDir__%findstr.exe /i /l /x /v /g:"%bin%\exclude_users.txt"
') do echo "%%~I"
wmic
输出通过管道传递到powershell
进行修剪,然后通过管道传递到findstr
。wmic
命令将使用where
子句排除禁用的帐户。根据需要更改
bin
的设置。
关于windows - 使用WMIC获取用户配置文件列表时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61623633/