Skip to content

Instantly share code, notes, and snippets.

@porteusconf
Last active November 11, 2020 05:07
Show Gist options
  • Save porteusconf/c4f8f670df524fedcdab473a357e79ba to your computer and use it in GitHub Desktop.
Save porteusconf/c4f8f670df524fedcdab473a357e79ba to your computer and use it in GitHub Desktop.
From output of various wmic commands creates plain-ascii (NOT unicode/utf) clean csv output with no blank lines.
@echo off
REM Creates wmic-min.txt with some minimal wmic info. And from .txt file creates clean .csv output
REM Usage: wmic2csv.bat > wmic-min.csv
REM Get REPL.BAT from https://github.com/numericOverflow/REPL.BAT
REM If csv output looks strange, take a look at wmic-min.txt file for clues.
REM The "| find /v " converts unicode (utf) to ansi (plain ascii text).
REM https://stackoverflow.com/questions/28672210/get-rid-of-spaces-and-tabs-in-wmic-output
wmic computersystem LIST brief /format:list | find /v "" > wmic-min.txt
wmic os get Caption,CSDVersion /format:list | find /v "" >> wmic-min.txt
REM From https://stackoverflow.com/questions/10945572/windows-batch-formatted-date-into-variable
REM You can get the current date in a locale-agnostic way using
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
REM Then you can extract the individual parts using substrings:
set today=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2%
echo YYYY-MM-DD=%today% >> wmic-min.txt
wmic bios get serialnumber /format:list | find /v "" >> wmic-min.txt
REM remove blank lines
REM findstr /v "^$" wmic-min.txt
REM replace the "=" with " ," to create csv output from wmic-min.txt file
findstr /v "^$" wmic-min.txt | REPL.bat "=" " ,"
@porteusconf
Copy link
Author

porteusconf commented Nov 11, 2020

I know wmic has a /format:csv option which can dump out one wide row with lots of columns. But the csv I wanted would look similar to the /format:table output (but with "," instead of "="), and with just two columns and maybe a dozen or so rows, as shown below.

Should work on any windows from XP to win10 with no 3rd-party exe needed. The only cheat I used was 3rd party repl.bat file but I'm sure there is a way to substitute "," for "=" with plain batch. But I wanted to use non-arcane easy-to-read commands. WMIC can be a real pain to parse so this is just a minimal-output example

C:>  wmic2csv.bat
Domain ,WORKGROUP
Manufacturer ,LENOVO
Model ,4282W2T
Name ,TTS-110199WL
PrimaryOwnerName ,student
TotalPhysicalMemory ,8354250752
Caption ,Microsoft Windows 7 Enterprise
CSDVersion ,Service Pack 1
YYYY-MM-DD ,2020-11-10
SerialNumber ,any1234

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment