Reputation: 13
Can I take the username and list products installed in the same txt file?
I thought of something like:
for /f "usebackq tokens=* delims=" %%a in (`wmic product get name`) do (
set "result=%%a"
)
echo %username%/%result% > test.txt
But no success.
I need this to put this on an Excel file, with another program, and I need this structure, username/programs.
Upvotes: 0
Views: 186
Reputation: 38642
You could probably use powershell from cmd or your batch-file, to get this information as a single line in CSV file in the format:
"UserName","Program Name 1","Program Name 2", "Program Name 3", etc…
Example command:
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile -Command "'\"' + (@($([System.Security.Principal.WindowsIdentity]::GetCurrent().Name.Split('\\')[-1])) + (Get-CimInstance -Class Win32_Product -Filter 'Name Is Not Null').Name -Join '\",\"') + '\"'" 1> "UserProducts.csv"
Upvotes: 0
Reputation: 80033
(
for /f "usebackq tokens=* delims=" %%a in (`wmic product get name`) do (
echo %username%/%%a
)
)> test.txt
Upvotes: 1
Reputation: 14340
You can absolutely do that, you just need to keep in mind that for
loops process one line at a time, so you need to output the value while you're still in the loop.
for /f "delims=" %%A in ('wmic product get name') do (
echo %%A >>test.txt
)
You don't need to set a variable for this since you aren't doing any string manipulation with it, but if you did, you would have to use delayed expansion:
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%A in ('wmic product get name') do (
set "line=%%A"
echo !line! >>test.txt
)
You can also skip the for
loop altogether and just redirect the output of the wmic
command straight to the text file:
wmic product get name >>test.txt
Upvotes: 0