2

I am trying to output the name and version of each program installed on my system to a .csv file. I could do something more complicated with VBScript, but would prefer to keep this very simple to include as a batch file that will run just before performing a system backup. After some research, I came up with the below:

wmic product get name,version /format:csv |more >Installed_Apps.csv

When I open the resulting file in Notepad, I see that it adds blank row at the top of the file and a node column. While neither are desired, I can live with both of these oddities. The thing that is bothering me, however, is that if I 2x click the file and open in Excel, there is a blank row between each row of results that I don't see when I open the file in Notepad.

Examples:

Notepad:

ComputerName,Software1,1.0
ComputerName,Software2,2.1
ComputerName,Software3,2.5

Excel:

ComputerName,Software1,1.0

ComputerName,Software2,2.1

ComputerName,Software3,2.5

I assume this is because the WMIC output was originally in Unicode, but I thought I was working around that correctly by adding |more to the command. From what I can tell, the file is in ASCII format, but I didn't inspect all the characters.

Any thoughts on what might be causing this and how to resolve this such that the file does not have the extra blank rows when opening in Excel?

Thanks!

1 Answer 1

3

The reason you're seeing blank rows when you use | more is that though the CSV's encoding is ANSI all right, it has extra Carriage Returns as you can see below:

1

If you omit the | more you end up with a Unicode CSV. Unfortunately Excel seems to have problems with such CSVs, in that on opening them the values aren't separated into columns. There are two solutions for this:

  1. If you don't care about losing Unicode (i.e. non-ANSI) characters, save the CSV with ANSI encoding before opening it with Excel.

  2. If you do care about the Unicode encoding, use Excel's Text Import Wizard (Data tab > From Text) with Comma specified as the delimiter:

    2


Edit: Here's the direct way to end up with an ANSI encoded CSV:

  1. Copy Windows\System32\wbem\en-US\csv.xsl (replace en-US with your language code) to any directory of your choice.

  2. Open the file in any text editor such as Notepad, then change the encoding attribute of the XML (XSLT) output method to Windows-1252 from utf-16, i.e. from this:

    <?xml version="1.0"?>
    <!-- Copyright (c) Microsoft Corporation.  All rights reserved. -->
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output encoding="utf-16" omit-xml-declaration="yes"/>
    

    to this:

    <?xml version="1.0"?>
    <!-- Copyright (c) Microsoft Corporation.  All rights reserved. -->
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output encoding="Windows-1252" omit-xml-declaration="yes"/>
    
  3. Run wmic product get name,version /format:csv.xsl > ProductList.csv

Note: Non-ANSI characters will be encoded as follows:

UTF-16:

ComputerName,用于 Visual Studio 2013 的 Microsoft 报告查看器加载项,11.1.3442.2

Windows-1252 (ANSI):

ComputerName,&#29992;&#20110; Visual Studio 2013 &#30340; Microsoft &#25253;&#21578;&#26597;&#30475;&#22120;&#21152;&#36733;&#39033;,11.1.3442.2
5
  • Thanks for the response, Karan. I guess I should have said I was already aware of the two manual ways you mentioned to resolve the issue, but I want to avoid anything manual. I am also aware that I can convert the file from Unicode to Ansi by removing the |more and then running the below command: cmd /a /c type Installed_Apps.csv>Installed_Apps2.csv, but I was hoping someone could point out a better way to do this in a single step (single line script). By the way, what tool did you use to show the line feeds above? Thanks again
    – Fred
    Commented May 5, 2015 at 16:08
  • I used Notepad++. Can you tell me why a single line script is necessary? Wouldn't creating a batch file suffice?
    – Karan
    Commented May 5, 2015 at 23:57
  • Thanks. It's not necessary. I would just prefer to write the correct answer to file once rather than writing a file, converting it, and deleting the original. If there is a more efficient way to fix the original syntax, or a better method all together, such that it writes the desired output without the added rows or having to write/convert/delete, I'd like to learn the technique/syntax.
    – Fred
    Commented May 6, 2015 at 13:48
  • See the edit above.
    – Karan
    Commented May 6, 2015 at 18:43
  • the wmic line that specifies /format:csv.xsl is a typo - it should just be /format:csv. Otherwise the solution provided works great!
    – Jason
    Commented Aug 28, 2019 at 21:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .