我正在使用以下批处理命令来检索所有本地用户配置文件(也包括域用户):
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
的设置。