Exclude some outputs from WMIC query - windows

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")

Related

Invalid alias verb (for /f)

I am learning to code the BATCH,
I would like to make a small .bat executable to retrieve various information on computers that I reinstall, to avoid having to navigate through several sections of the windows control panel.
I have already managed to retrieve some information using "for /f", but I must admit that I still don't understand how this command works.
I haven't found any more info on what I'm looking for, so I'm asking for help which would be very welcome.
I'm not looking for a copy/paste solution, I'd like someone to explain my mistake and help me solve it so I can better understand where I went wrong.
Anyway, after reading this indigestible pamphlet, here are the commands I'm looking to run:
for /f "tokens=2 delims==" %%I in (
'wmic LOGICALDISK where DriveType!=4 get DeviceID /value'
) do for /f "delims=" %%# in ("%%I") do set "deviceid=%%~#"
for /f "tokens=2 delims==" %%I in (
'wmic LOGICALDISK where drivetype!=4 get description /value'
) do for /f "delims=" %%# in ("%%I") do set "description=%%~#"
for /f "tokens=2 delims==" %%I in (
'wmic LOGICALDISK where drivetype!=4 get volumename /value'
) do for /f "delims=" %%# in ("%%I") do set "volumename=%%~#"
for /f "tokens=2 delims==" %%I in (
'wmic LOGICALDISK where drivetype!=4 get Size /value'
) do for /f "delims=" %%# in ("%%I") do set "Size=%%~#"
and this is what I get back:
C:\Users\rv\Desktop>for /F "tokens=2 delims==" %I in ('wmic LOGICALDISK where DriveType! 4 get DeviceID /value') do for /F "delims=" %# in ("%I") do set "deviceid=%~#"
4 - Verbe de l'alias non valide.
C:\Users\rv\Desktop>for /F "tokens=2 delims==" %I in ('wmic LOGICALDISK where drivetype! 4 get description /value') do for /F "delims=" %# in ("%I") do set "description=%~#"
4 - Verbe de l'alias non valide.
C:\Users\rv\Desktop>for /F "tokens=2 delims==" %I in ('wmic LOGICALDISK where drivetype! 4 get volumename /value') do for /F "delims=" %# in ("%I") do set "volumename=%~#"
4 - Verbe de l'alias non valide.
C:\Users\rv\Desktop>for /F "tokens=2 delims==" %I in ('wmic LOGICALDISK where drivetype! 4 get Size /value') do for /F "delims=" %# in ("%I") do set "Size=%~#"
4 - Verbe de l'alias non valide.
I think this is due to a misuse of "tokens=" and "delims=" on my part ?
Mmm... I used to think/solve this type of problems in "opposite order"... Please, follow me.
I started typing this command at the command-prompt:
C:\> wmic LOGICALDISK where DriveType!=4 get DeviceID /value
DeviceID=C:
DeviceID=D:
Ok. I always try to make things in an efficient way. If I can get the same result from a single command instead of 4 commands, then I always opted for the single one:
C:\> wmic LOGICALDISK where DriveType!=4 get DeviceID,Description,VolumeName,Size /value
Description=Local Fixed Disk
DeviceID=C:
Size=982768414720
VolumeName=Windows
Description=Local Fixed Disk
DeviceID=D:
Size=16111366144
VolumeName=RECOVERY
Good! In order to "get" such values, we need to execute the wmic command from a for /F command, so I wrote this simple Batch file at first:
for /F "delims=" %%a in ('wmic LOGICALDISK where DriveType!=4 get DeviceID,Description,VolumeName,Size /value') do #echo [%%a]
... and the problems started:
C:\> for /F "delims=" %a in ('wmic LOGICALDISK where DriveType! 4 get DeviceID Description VolumeName Size /value') do #echo [%a]
4 - Verbo de alias no válido.
If you compare the source line vs. the executed command, you'll realize that the equal-sign and the commas disappeared! This happens because these characters: = , ; works as separators for items in most commands (besides space and TAB). This is this way just because MS (Microsoft) decided that... :(
There are two ways to solve this problem: individually caret-^escape each one of these characters OR enclose the whole command in quotes. I choose the second method because it is simpler:
for /F "delims=" %%a in ('"wmic LOGICALDISK where DriveType!=4 get DeviceID,Description,VolumeName,Size /value"') do #echo [%%a]
And the output is:
C:\> for /F "delims=" %a in ('"wmic LOGICALDISK where DriveType!=4 get DeviceID,Description,VolumeName,Size /value"') do #echo [%a]
]
]
]Description=Local Fixed Disk
]DeviceID=C:
]Size=982768414720
]VolumeName=Windows
]
]
]Description=Local Fixed Disk
]DeviceID=D:
]Size=16111366144
]VolumeName=RECOVERY
]
]
]
So far so good, we have solved the equal-sign and comma problem... However, a new problem arises: the lines we think were empty really contain a CR character! There are a couple methods to solve this new point, and the one you used before is the simplest one: process the wmic output lines in a second FOR command:
for /F "delims=" %%a in (
'"wmic LOGICALDISK where DriveType!=4 get DeviceID,Description,VolumeName,Size /value"'
) do #(
for /F "delims=" %%b in ("%%a") do #echo [%%b]
)
And the output is:
C:\> for /F "delims=" %a in ('"wmic LOGICALDISK where DriveType!=4 get DeviceID,Description,VolumeName,Size /value"') do #(for /F "delims=" %b in ("%a") do #echo [%b]
)
[Description=Local Fixed Disk]
[DeviceID=C:]
[Size=982768414720]
[VolumeName=Windows]
[Description=Local Fixed Disk]
[DeviceID=D:]
[Size=16111366144]
[VolumeName=RECOVERY]
Perfect! Now, that we are sure that the code will process the values we want, we insert an #echo off command at beginning and split each line in two parts, the variable and its value:
#echo off
for /F "delims=" %%a in (
'"wmic LOGICALDISK where DriveType!=4 get DeviceID,Description,VolumeName,Size /value"'
) do (
for /F "tokens=1,2 delims==" %%b in ("%%a") do echo [%%b=%%c]
)
The output is the same than before.
Finally, we insert code to save the value of each variable and process all variables each time that a group is complete; that is:
#echo off
setlocal EnableDelayedExpansion
set "VolumeName="
for /F "delims=" %%a in (
'"wmic LOGICALDISK where DriveType^!=4 get DeviceID,Description,VolumeName,Size /value"'
) do (
for /F "tokens=1,2 delims==" %%b in ("%%a") do set "%%b=%%c"
if defined VolumeName (
echo/
echo ===== New disk =====
echo Description=!Description!
echo DeviceID=!DeviceID!
echo Size=!Size!
echo VolumeName=!VolumeName!
set "VolumeName="
)
)
The final output:
===== New disk =====
Description=Local Fixed Disk
DeviceID=C:
Size=982768414720
VolumeName=Windows
===== New disk =====
Description=Local Fixed Disk
DeviceID=D:
Size=16111366144
VolumeName=RECOVERY
Note that in the last code the exclamation-mark needs to be ^escaped. This is because the EnableDelayedExpansion setlocal's option enables the ! as a method to expand variables additional to the %standard% one. You can find a lot of questions/answers about this topic in this site...
How-to: Escape Characters, Delimiters and Quotes at the Windows command line.
In Windows cmd/batch scripting, parameters are most often separated by spaces, but any of the following are also valid delimiters:
, (U+002C, Comma)
; (U+003B, Semicolon)
= (U+003D, Equals Sign)
(U+0020, Space)
␉ (U+0009, Character Tabulation)
You need to escape the Equals Sign in DriveType!=4. Use any of the following:
"DriveType!=4" (using "Double Quotes")
DriveType!^=4 (using common escape character ^ (U+005E, Circumflex Accent))

Viewing a list of Installed Hotfixes/Patches using wmic (HotfixID, InstalledOn and Description)

I found this post with regard to getting a list of installed Hotfixes using wmic qfe list full and #Hackoo replied with the following:
#echo off
Title wmic to get HotfixID
Setlocal EnableDelayedExpansion
echo "patches" : {
set "patches=wmic qfe get HotfixID"
for /f "skip=1" %%i in ('%patches%') do for /f "delims=" %%j in ("%%i") do (
set /a count=count+1
echo "!count!" : "%%j",
)
echo }
This works absolutely fine but is it also possible to incorporate the Description and InstalledOn wmic information as well so that the output displays the following:
HotfixID InstalledOn Description
Using the above code I can get each individually but not all together as the InstalledOn / Description seem to repeat the first value.
I then stepped right outside my level of knowledge and tried the following (which does not work):
for /f "tokens=1,2,3 skip=1 delims=," %%a in ('%SystemRoot%\System32\wbem\wmic.exe qfe get HotfixID,InstalledOn,Description') do (
set "hotfix_number=%%~a"
set "hotfix_installed=%%~b"
set "hotfix_description=%%~c"
)
echo %hotfix_number% installed on %hotfix_installed% - %hotfix_description%
Here's hoping you are able to assist.
Does this help?
#For /F "Skip=2 Tokens=1,* Delims=," %%G In ('%SystemRoot%\System32\wbem\WMIC.exe QFE Get Description^, HotFixID^, InstalledOn /Format:CSV 2^>NUL') Do #For /F "Tokens=1-3 Delims=," %%I In ("%%H") Do #Echo %%J installed on %%K - %%I

Issues when getting a list of user profiles using WMIC

I was using the following batch command to retrieve all local user profiles (including domain users too) :
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 (
The problem is that this command has its limits: it doesn't really check if the users in question do actually have an account.
The user Compo provided me a methodology for retrieving the profile names, using WMIC.
So I ended up writing the following command:
#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 (
The problem is: it ignores my exclusion file (which contains one user per line) and it ends up a profile without any name.
Any idea How I can solve these issues ?
#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"
The wmic output is piped to powershell to be trimmed and then piped to findstr.
The wmic command will exclude disabled accounts by use of the where clause.
Change the setting of bin as needed.
If you want a solution which still uses WMIC, and your exclusion list, then the following should do as you require.
#For /F Tokens^=4Delims^=^" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount Where "LocalAccount='TRUE'" Assoc:List /ResultRole:Name 2^>NUL')Do #Echo %%G|%__AppDir__%findstr.exe /VXLIG:"%~dp0exclude_users.txt"
You can split that over multiple lines for easier reading too:
#For /F Tokens^=4Delims^=^" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount^
Where "LocalAccount='TRUE'" Assoc:List /ResultRole:Name 2^>NUL'
)Do #Echo %%G|%__AppDir__%findstr.exe /VXLIG:"%~dp0exclude_users.txt"

store part of outcome command to variable

Hello dear people and others,
Today i wanted to create a simple script, thought it would be easy to store the outcome to var of the following command:
wmic bios get serialnumber | findstr /N /V SerialNumber
Outcome:
2:H3GK4S1
3:
The problem is when i try to get the serial with wmic, it returns the string as expected but also an empty string/line. When i try to store the serial to a variable it stores it and then directly overwrites it with the empty string. This is the function i nearly got working now:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber ^| FINDSTR /N /V SerialNumber') DO (SET serial=%g & ECHO %g)
And this gives the following output:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber ^| FINDSTR /N /V SerialNumber') DO (SET serial=%g & ECHO %g)
2:H3GK4S1
3:
As can be seen above, the loop overwrites the serial var, if someone can help me towards the right directon to get this working, would be mad.
At the Command Prompt:
For /F "Tokens=1* Delims==" %g In ('WMIC BIOS Get SerialNumber /Value') Do #For /F "Tokens=*" %i In ("%h") Do #Set "serial=%i" & Echo %i
Or in a batch file:
#For /F "Tokens=1* Delims==" %%g In ('WMIC BIOS Get SerialNumber /Value'
) Do #For /F "Tokens=*" %%i In ("%%h") Do #Set "serial=%%i" & Echo %%i
#Pause
EditIf you're happy to use a labelled section in your batch file:
#Echo Off
Set "serial="
For /F "Skip=1 Delims=" %%A In ('WMIC BIOS Get SerialNumber') Do If Not Defined serial Call :Sub %%A
Set serial 2>Nul
Pause
GoTo :EOF
:Sub
Set "serial=%*"
GoTo :EOF
Try like this:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber /format:value') DO for /f "tokens=* delims=" %# in ("%g") do set "serial=%#"
echo %serial%
Mind that's a command that should be executed in the command prompt directly.For a batch file you'll need to double the % in the for loop tokens.
In a batch file, you can also use a goto to end the loop after the first iteration :
#echo off
for /f "tokens=2 delims=:" %%a in ('wmic bios get serialnumber ^| findstr /N /V SerialNumber') do (
set "$var=%%a"
goto:next
)
exit/b
:next
echo Result=^> [%$var: =%]

How can I remove empty lines from wmic output?

This is my string to obtain the position of TeamViewer (any version) service executable:
for /f "skip=1 delims=" %A in ('wmic path win32_service where "name like 'TeamViewer%'" get pathname') do set POSITION=%A
The problem is caused by wmic because it includes an empty line at the end of result (on Windows 7 command) and this is the output:
C:\Users\giovanni>for /f "skip=1 delims=" %A in ('wmic path win32_service where "name like 'TeamViewer%
'" get pathname') do set POSITION=%A
:\Users\giovanni>set POSITION="C:\Program Files\TeamViewer\Version8\TeamViewer_Service.exe"
:\Users\giovanni>set POSITION=
C:\Users\giovanni>echo %position%
ECHO enabled.
How I can get only the second line of the output with the correct position of the executable? (or skip the latest line, of course).
Thanks all in advance and have a nice day.
Giovanni.
This is checktv.bat:
for /f "skip=1 delims=" %%A in ('wmic path win32_service where "name like 'TeamViewer%'" get pathname ^| findstr /r /v "^$"') do set POSITION=%%A
echo %POSITION%
Like this:
for /f "skip=1 delims=" %A in (
'wmic path win32_service where "name like 'TeamViewer%'" get pathname ^| findstr /r /v "^$"'
) do set POSITION=%A
The findstr /r /v "^$" removes empty lines from the output.
wmic blah /value | find "=" >> wherever
output will be
field=value
no extra lines
tokenize from there, delim =

Resources