Adding a comma at the end of each line wmic - windows

I am trying to get a comma seperated list of printers my computer has access to. So far I have #echo off & for /f "delims=" %i in ('wmic printer get name ^| findstr /v "Name"') do echo %i, but for some reason it's replacing the first letter of each printer with a comma. Changing the %i, in the end is slightly better, not replacing the first letter with a comma but just adding one in front of the name of the printer instead. Though this is better this is not what I'm looking for because I want to get the output.
PrinterOne,
Printer2,
Printer3,
etc,
I'm pretty sure %i, should work, or am I wrong?
Side note I've had a working solution for months running
For /F "Tokens=1,* EOL=' Delims=," %%G In ('%%SystemRoot%%\System32\wbem\WMIC.exe Printer Get Name^ /Format:CSV 2^>NUL') Do #For /F "Tokens=*" %%I In ("%%H") Do #Echo %%I,
from a batch file, but as it looks it does not work with some of the computers on our system. Could be windows related but I'm not sure.

I managed this with
powershell -Command "& { Get-Printer -ComputerName $env:COMPUTERNAME | Select-Object -ExpandProperty Name | foreach-object {$_ + ','}}"

Related

How to get the content of a text file using command FOR on a batch file

I have a text file where the contents have a lot of NUL or spaces between data:
[nul][nul][nul][nul]Name [nul][nul][nul][nul][nul][nul]surname
The data inside of the text file is always different.
I have searched and tested many similar questions using for /F "usebackq delims=" %%a in ("%file%") do echo %%a and similar commands, but i always get empty results.
Please can anyone help?
If you truly have a text file with, for example, the following content:
External image link
And you just want to omit the NUL characters as part of a normal For /F loop file read, then you could ask powershell for help from your batch-file:
#For /F "Delims=" %%G In (
'%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe ^
-Nologo -NoProfile -Command "(Get-Content 'sourcefile.txt')" ^
" -replace '\x00',''"') Do #Echo(%%G
#Pause
I have split that long line up into multiple for easier reading, but you could also have it as a single line batch-file:
#(For /F "Delims=" %%G In ('%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -Nologo -NoProfile -Command "(Get-Content 'sourcefile.txt') -replace '\x00',''"') Do #Echo(%%G) & Pause
And to do it in simple terms directly from the Command Prompt cmd:
For /F "Delims=" %G In ('powershell -NoP "(GC 'sourcefile.txt') -replace '\x00',''"') Do #Echo(%G
[Edit /]
If you wanted to add a single comma, between those two specific words in the exact example you provided, and which I used in my linked image above, then yes, it would be possible.
For example you could do it within the PowerShell part, by changing:
-replace '\x00','' to -replace ' ',', ' -replace '\x00',''
Or, you could do it by changing the For loop option:
"Delims=" to "Tokens=1*"
and then change either:
#Echo(%%G, or #Echo(%G
to:
#Echo(%%G, %%H or #Echo(%G, %H respectively.

Get command prompt out on one line

I am trying to get Model and Size of all local disks.
I want to output on one line for each disk.
Example: Model Samsung Size 500GB
#echo off
for /f "tokens=2 delims==" %%d in ('wmic diskdrive get Model,Size /value') do (
set Model=%%d
set Size=%%d
)
echo Model %Model% Size %Size%
pause
But nothing.
As model descriptions may contain spaces, you need to format the output as csv, so the output is delimited by commas (I hope there aren't model descriptions that contain commas - I didn't see one so far).
Without /value each disk is listed in one line (Node,Model,Size), so you need tokens=2,3. Add a skip=2 to remove the header line and add your fixed strings to the final output:
for /f "skip=2 tokens=2,3 delims=," %%a in ('"wmic diskdrive get Model,Size /format:csv"') do #echo Model %%a Size %%b
Here is one way to do it on all current day, supported Windows machines. The stated goal is "one line." If you want model and size in separate variables, there is more to do.
powershell -NoLogo -NoProfile -Command ^
"Get-CimInstance -ClassName CIM_DiskDrive |" ^
"ForEach-Object { 'Model {0} Size {1}' -f #($_.Model, $_.size) }"

Detecting Removable drive letter in CMD

I'm trying to write a script, which will detect the letter of my USB Removable Drive called "UUI" and then create folder on it. I've written few commands for CMD which, when run separately, work. However when I put them into a bat file, I always get some errors. Here are the commands in a bat file:
for /F "tokens=1 delims= " %i in ('WMIC logicaldisk where "DriveType=2" list brief ^| c:\windows\system32\find.exe "UUI"') do (echo %i > drive.txt)
set /p RemovableDriveLetter2= < drive.txt
del /F /Q drive.txt
set RemovableDriveLetter=%RemovableDriveLetter2:~0,1%
%RemovableDriveLetter%:
md MyNewFolder
cd MyNewFolder
When I go to cmd.exe and run the file by calling "myScript.bat" or "call myScript.bat", I get an error:
C:\Users\UUI\Desktop>myScript.bat
\windows\system32\find.exe was unexpected at this time.
C:\Users\UUI\Desktop>for /F "tokens=1 delims= " \windows\system32\find.exe "UUI"') do (echo i > drive.txt)
C:\Users\UUI\Desktop>
I can see that MyNewFolder was not created. However, when I copy all lines and run them in CMD as such (e.g. not in the .bat file) and run them one by one, it is fully functional within the cmd.exe instance.
How can I create bat a file, which will successfully run and detects the drive letter of my removable drive without issues? Or how can I solve the error "\windows\system32\find.exe was unexpected at this time."?
You need to double the % sign used to mark a FOR loop control variable in a batch script (.bat or .cmd), i.e. use %%i instead of %i used in pure CLI.
However, there is another possible approach how-to parse wmic output.
See also Dave Benham's WMIC and FOR /F: A fix for the trailing <CR> problem
#echo OFF
SETLOCAL enableextensions
set "USBCounter=0"
for /F "tokens=2 delims==" %%G in ('
WMIC logicaldisk where "DriveType=2" get DeviceID /value 2^>NUL ^| find "="
') do for /F "tokens=*" %%i in ("%%G") do (
set /A "USBCounter+=1"
echo %%i
rem your stuff here
)
echo USBCounter=%USBCounter%
rem more your stuff here
ENDLOCAL
goto :eof
Here the for loops are
%%G to retrieve the DeviceID value;
%%i to remove the ending carriage return in the value returned: wmic behaviour: each output line ends with 0x0D0D0A (CR+CR+LF) instead of common 0x0D0A (CR+LF).
One could use Caption or Name instead of DeviceID:
==>WMIC logicaldisk where "DriveType=2" get /value | find ":"
Caption=F:
DeviceID=F:
Name=F:
Note there could be no or more disks present having DriveType=2:
==>WMIC logicaldisk where "DriveType=2" get /value | find ":"
No Instance(s) Available.
==>WMIC logicaldisk where "DriveType=2" list brief
DeviceID DriveType FreeSpace ProviderName Size VolumeName
F: 2 2625454080 3918512128 HOMER
G: 2 999600128 1029734400 LOEWE
Script output for no, then one and then two USB drive(s), respectively:
==>D:\bat\SO\31356732.bat
USBCounter=0
==>D:\bat\SO\31356732.bat
F:
USBCounter=1
==>D:\bat\SO\31356732.bat
F:
G:
USBCounter=2
==>

Powershell or batch script for collecting username, pcname, and Dell service tag(wmic identifyingname)

I have been tasked with the job of collecting data from around 3-400 pcs and laptops. The task has to be done locally one by one on each machine for security reasons.
Can someone point me in the right direction of using a powershell or batch script that will get the details.
I tried using the following batch script:
for /f "skip=1 tokens=1" %%A in ('wmic csproduct get identifyingnumber /value') do set asset=%%A
echo %%A,%computername%,%username% >> "file.csv"
but this didnt work as the wmic command has a blank line at the end and overwrites the %%A with blank.
Please help a noob :)
Thanks in advance.
for /f "tokens=2 delims==" %%a in ('
wmic csproduct get identifyingnumber /value
') do for /f "delims=" %%b in ("%%a") do (
>>"file.csv" echo(%%b,%computerName%,%username%
)
The /value swith in wmic command asks for output in key=value format. The for will use the equal sign as a delimiter to tokenize the line, retrieving only the second token, that is, the value. As we are requesting the second token, and the only line that will contain two tokens is the line with the data, there is no need to skip or filter the output.
Also, the output from wmic contains an aditional carriage return at the end of the line so a second for loop is used to remove it.
edited to adapt to comments
#echo off
setlocal enableextensions disabledelayedexpansion
for /f "delims=" %%a in ('
wmic csproduct get identifyingnumber^,name^,vendor /value
') do for /f "delims=" %%b in ("%%a") do set "_%%b"
for /f "delims=" %%a in ('
wmic useraccount where (name^="%username%"^) get fullname /value
') do for /f "delims=" %%b in ("%%a") do set "_%%b"
>>"file.csv" echo(%_fullname%,%username%,%_vendor%,%_name%,%computerName%,%_identifyingnumber%
Simplified approach in this case. We are still retrieving the key=value format, so, use this format to directly set a environment variable with the value. An aditional query is needed to retrieve the full name for the current user (note: obviously the data is retrieved for the user running the script)
You have to manually due this on every machine? That seems kinda crazy when a single WMI call can get this remotely with no problem.
Here's an easier way to do it with Powershell locally.
Get-WmiObject -Class Win32_Bios | Select-Object PSComputerName,SerialNumber | Export-Csv -Path computerserials.csv -Append -NoTypeInformation
Add the -Computername param in there to do it remotely.
Get-WmiObject -Class Win32_Bios -Computername COMPUTERNAME | Export-Csv -Path computerserials.csv -Append -NoTypeInformation
To iterate over a bunch of computer names create a text file with a single computer name per line and do something like this but that would require you do it remotely.
Get-WmiObject -Class Win32_Bios -Computername (Get-Content computerlist.txt) | Select-Object PSComputerName,SerialNumber | Export-Csv -Path computerserials.csv -Append -NoTypeInformation
These examples are getting the computer name and the serial number. If you need anything else its really easy to pull it directly out of WMI instead of using the kludge that is WMIC.

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