I'm getting the list of Windows users and their local path, through wmic (thanks Compo).
I would like some user names to be excluded from the output in this wmic command :
@For /F "Skip=1Tokens=1,2" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount Where^
"LocalAccount='True' And Not Name Like '[_]%%'" Get Name^,SID 2^>Nul'
)Do @For /F %%I In ("%%H")Do @For /F "Tokens=2Delims==" %%J In ('
%__AppDir__%wbem\WMIC.exe Path Win32_UserProfile Where^
"SID='%%I' And Special!='True'" Get LocalPath /Value 2^>Nul'
)Do @For /F "Tokens=*" %%K In ("%%J")Do @Echo User name:"%%G",Profile path:"%%K"
I'm not sure how I can add this exclusion file :
%__AppDir__%findstr.exe /V /X / L/ /I G:"usernames.txt"
Can you help me please ?
Thank you
If your intention is to exclude names from the output, the general rule for efficiency, is to filter your commands as soon as possible in your code.
For this reason, the most efficient method would be to make the individual exclusions within the Where
clause. I provided an example of how to do that in my comment, e.g. change the current exclusion, of names beginning with an underscore, (And Not Name Like '[_]%%'
), to And Name!='Dimitri' And Name!='Kalinda' And Name!='Peter'
.
If you have a list of exclusions one per line in a file, and there are too many to transfer into the Where
clause, then you should perform that filtering in the Do
portion of that initial For
loop. You could at that point use findstr.exe
with the options you chose, (just fixed).
As the code you chose from my original answer was not the robust one, which caters for user names with spaces/problematic characters, I'd suggest you change to that too.
For that reason, this would be my suggested answer, (excluding names within usernames.txt
using findstr.exe
):
@For /F Tokens^=4Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe
UserAccount Where "LocalAccount='TRUE'" Assoc:List /ResultRole:SID 2^>NUL'
)Do @Set /P "=%%G"<NUL|%SystemRoot%\System32\findstr.exe /XVLIG:"usernames.txt"^
>NUL&&(For /F "Tokens=1*Delims==" %%H In ('%SystemRoot%\System32\wbem\WMIC.exe
UserAccount Where "Name='%%G'" Get SID /Value 2^>NUL
^|%SystemRoot%\System32\find.exe "="')Do @For %%J In (%%I
)Do @For /F "Tokens=1*Delims==" %%K In ('%SystemRoot%\System32\wbem\WMIC.exe
Path Win32_UserProfile Where (SID^="%%J" And Special!^="TRUE" And LocalPath
Is Not Null^) Get LocalPath /Value 2^>NUL^|%SystemRoot%\System32\find.exe
"="')Do @For /F Tokens^=* %%M In ("%%L"
)Do @Echo UserName:"%%G", UserProfile:"%%M")