Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 在批处理文件中获取用户名和产品_Windows_Batch File_Cmd_Powershell - Fatal编程技术网

Windows 在批处理文件中获取用户名和产品

Windows 在批处理文件中获取用户名和产品,windows,batch-file,cmd,powershell,Windows,Batch File,Cmd,Powershell,我可以使用用户名并列出安装在同一txt文件中的产品吗 我想到了这样的事情: for /f "usebackq tokens=* delims=" %%a in (`wmic product get name`) do ( set "result=%%a" ) echo %username%/%result% > test.txt 但是没有成功 我需要用另一个程序将它放在Excel文件中,我需要这个结构,用户名/程序。你完全可以这样做,

我可以使用用户名并列出安装在同一txt文件中的产品吗

我想到了这样的事情:


for /f  "usebackq tokens=* delims=" %%a in (`wmic product get name`) do (
    set "result=%%a"
)

echo %username%/%result% > test.txt
但是没有成功


我需要用另一个程序将它放在Excel文件中,我需要这个结构,用户名/程序。

你完全可以这样做,你只需要记住,
for
循环一次只处理一行,所以你需要在循环中输出值

for /f "delims=" %%A in ('wmic product get name') do (
    echo %%A >>test.txt
)
您不需要为此设置变量,因为您没有使用它执行任何字符串操作,但如果您这样做,则必须使用延迟扩展:

@echo off
setlocal enabledelayedexpansion

for /f "delims=" %%A in ('wmic product get name') do (
    set "line=%%A"
    echo !line! >>test.txt
)

您也可以跳过
for
循环,只需将
wmic
命令的输出直接重定向到文本文件:

wmic product get name >>test.txt
您可能可以使用from或your,以CSV文件中的单行格式获取此信息,格式如下:

"UserName","Program Name 1","Program Name 2", "Program Name 3", etc…
命令示例:

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