Search code examples
outputwmic

WMIC output to text file


I have a script that I run on all pc's that I image and it basically looks and tells me if a program is installed and if so, what version. When I run my command in a CMD window it works. But as part of my script, it deletes everything in the txt file and adds weird characters. Here is the Edge code.

:Edge
If  exist "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"  (
 echo Edge is installed >> C:\Temp\Message.txt
 SET "EDGE=Y"
 wmic /OUTPUT:"C:\Temp\Message.txt" datafile where 'name="C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"' get version 
) Else (
SET "EDGE=N"&echo Edge is Not installed >> C:\Temp\Message.txt
)

Here is the result Version 94.0.992.50 ?????????????4???????????????????????????????????????????????????????????

I am at a loss.....


Solution

  • You can give a try with this batch script that use a powershell command in one line :

    (Get-AppxPackage -Name "Microsoft.MicrosoftEdge.Stable").Version
    

    Tested only on Windows 10 (32 bits)

    @echo off
    Title Get Edge Version with Powershell and Batch
    Set PassPSCMD="(Get-AppxPackage -Name "Microsoft.MicrosoftEdge.Stable").Version"
    Set "LogFile=C:\Temp\Message.txt"
    Call :RunPS %PassPSCMD% EdgeVersion
    >"%LogFile%" (
        If defined EdgeVersion (
            echo Edge has a version : %EdgeVersion%
        ) else (
            echo Edge is not Installed !
        )
    )
    If Exist "%LogFile%" Start "" /MAX "%LogFile%"
    Exit
    REM -----------------------------------------------------------------------------
    :: A function that would execute powershell script and return a value from it.
    :: <RetValue> the returned value to be set like in this case we need Edge Version
    :RunPS <PassPSCMD> <RetValue>
    @for /F "usebackq tokens=*" %%i in (`Powershell -C %1`) do set "%2=%%i"
    Goto:eof
    :: End of :RunPS function
    REM -----------------------------------------------------------------------------
    

    Or with Reg Query in Pure Batch is faster than the previous script

    Tested only on Windows 10 (32 bits)

    @echo off
    Title Get Edge Version with Reg Query in Pure Batch
    Set "LogFile=C:\Temp\Message.txt"
    Set "MyKey=HKEY_CURRENT_USER\SOFTWARE\Microsoft\Edge\BLBeacon"
    
    @for /f "tokens=3 delims= " %%a in (
        'Reg Query "%MyKey%" 2^>nul ^| find /I "Version"'
    ) do (Set "EdgeVersion=%%a")
    
    >"%LogFile%" (
        If defined EdgeVersion (
            echo Edge has a version : %EdgeVersion%
        ) else (
            echo Edge is not Installed !
        )
    )
    If Exist "%LogFile%" Start "" /MAX "%LogFile%" & Exit
    

    And if you insist to use WMIC give a shot for this batch script :

    @echo off
    Title Get Edge Version with WMIC
    Set "LogFile=C:\Temp\Message.txt"
    SET "ARCH=x64" 
    IF /I "%PROCESSOR_ARCHITECTURE%"=="x86" (IF NOT DEFINED PROCESSOR_ARCHITEW6432 SET "ARCH=x86")
    If /I "%ARCH%"=="x86" ( set "PrgFiles=%ProgramFiles%" ) else ( set "PrgFiles=%ProgramFiles(x86)%" )
    Set EdgePath=%PrgFiles%\Microsoft\Edge\Application\msedge.exe
    echo( & echo Getting Version of "%EdgePath%"
    Call :GetVersion "%EdgePath%" Version
    echo EDGE Version : %Version%
    echo EDGE Version : %Version%>>"%LogFile%"
    Timeout /T 3 /NoBreak>nul
    If Exist "%LogFile%" Start "" /MAX "%LogFile%" & Exit /B
    ::----------------------------------------------------------------------------------------
    :GetVersion <App> <Version>
    Rem The argument %~1 represent the full path of the application without the double quotes
    Rem The argument %2 represent the variable to be set (in our case %2=Version)
    Set "App=%~1"
    Set "App=%App:\=\\%"
    FOR /F "tokens=2 delims==" %%I IN (
        'wmic datafile where "name='%App%'" get version /Value 2^>^nul'
    ) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
    Exit /b
    ::----------------------------------------------------------------------------------------