I am checking for the OS version using command prompt.
I am using following code to get the version number
systeminfo | findstr /B /c:"OS Version"
Output
For Windows Server 2003
OS Version: 5.2.3790 Service Pack 2 Build 3790
For Windows 7
OS Version: 6.1.7601 Service Pack 1 Build 7601
But I am not sure how can I have Major version number (First digit of version)
How can I get the major version number OR
If can I check if the version numbers first char is 5 OR 6
You could pipe the output again through FINDSTR, for example:
systeminfo | findstr /B /c:"OS Version" | findstr /c:" 6."
And then check the ERRORLEVEL returned to see if 6. was present in the returned string.
NOTE:
I've put a space at the beginning of the second string to try and ensure that it doesn't match against anything else that might be stuffed into the output from systeminfo on an "OS Version" line
This is really brittle as it relies on the format of the returned text from systeminfo
There's doubtless a better way to do this with the VER command
For a really robust way of doing this that is independent of the format of the text returned by systeminfo/ver, consider writing a small program in C# (Express Edition is free) that looks at the System.Environment.OSVersion property and sets an error level for you as this would be string parse-free (and thus a lot less likely to break)!
here is a snippet using ver.exe :
for /f "tokens=4 delims=. " %%i in ('ver') do set majorversion=%%i
I would use the ver command instead of systeminfo. My Windows 7 machine reports:
Microsoft Windows [Version 6.1.7601]
The following batch script snippet will define a variable containing the primary version number:
for /f "tokens=2 delims=[]" %%A in ('ver') do for /f "tokens=2 delims=. " %%B in ("%%A") do set "majorVersion=%%B"
Related
How can I output each command lines to one single file but keep monitoring the results at the same command.
#echo off
Title %~n0
if not "%1" == "max" start /MAX cmd /c %0 max & exit/b
Echo Hard Disk Info
set record="C:\%computername%.txt"
Echo.
powershell "get-physicaldisk">C:\%computername%.txt>con
echo=================================
Echo.
Echo CPU Info
Echo.
wmic cpu get caption, name
echo=================================
Echo.
Echo RAM Info
Echo.
wmic memorychip get capacity,memorytype,speed,typedetail,manufacturer
echo=================================
echo.
Echo Windows Version
Echo.
systeminfo | findstr /B /i /C:"OS Name" /C:"OS Version" /C:"System Type" /C:"System Locale" /C:"Input Locale"
echo=================================
Echo.
Echo Office Version
echo.
Echo LCID = 1033-English(US)
wmic product where "Name like '%%Office%%'" get language,name, version
Pause
#exit %0
The following code could be used to write everything into a file and display also in the console window:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
title %~n0
if not "%~1" == "max" start /MAX %SystemRoot%\System32\cmd.exe /D /C %0 max & exit /B
set "RecordFile=%UserProfile%\%ComputerName%.txt"
del "%RecordFile%" 2>nul
set "TempFile=%TEMP%\%~n0.tmp"
call :OutputInfo "Hard Disk Info"
call :OutputData %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe "Get-PhysicalDisk"
call :OutputInfo "CPU Info"
call :OutputData %SystemRoot%\System32\wbem\wmic.exe CPU GET Caption,Name
call :OutputInfo "RAM Info"
call :OutputData %SystemRoot%\System32\wbem\wmic.exe MEMORYCHIP GET Capacity,MemoryType,Speed,TypeDetail,Manufacturer
call :OutputInfo "Windows Version"
call :OutputData %SystemRoot%\System32\cmd.exe /D /S /C "%SystemRoot%\System32\systeminfo.exe 2>nul | %SystemRoot%\System32\findstr.exe /B /I /C:"OS Name" /C:"OS Version" /C:"System Type" /C:"System Locale" /C:"Input Locale""
call :OutputInfo "Microsoft Office Version"
echo LCID 1033 = English (US)>>"%RecordFile%"
echo LCID 1033 = English (US)
call :OutputData %SystemRoot%\System32\wbem\wmic.exe PRODUCT where "Name like '%%%%Microsoft Office%%%%'" GET Language,Name,Version
del "%TempFile%" 2>nul
echo/
pause
exit /B
:OutputData
%* >"%TempFile%"
for %%I in ("%TempFile%") do if %%~zI == 0 goto :EOF
type "%TempFile%">>"%RecordFile%"
type "%TempFile%"
goto :EOF
:OutputInfo
if not exist "%RecordFile%" goto InfoOutput
(echo =================================& echo/)>>"%RecordFile%"
echo =================================
echo/
:InfoOutput
(echo %~1&echo/)>>"%RecordFile%"
echo %~1
echo/
goto :EOF
Note 1:
The PowerShell command line outputs on Windows 7 with by default installed PowerShell 2.0 just the following error message because of the cmdlet Get-PhysicalDisk is not available with PowerShell 2.0.
The term 'Get-PhysicalDisk' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At line:1 char:17
+ Get-PhysicalDisk <<<<
+ CategoryInfo : ObjectNotFound: (Get-PhysicalDisk:String) [], Co
mmandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Note 2:
WMIC outputs data always Unicode encoded using UTF-16 Little Endian (two or four bytes per character) with byte order mark (BOM) while PowerShell and SystemInfo output data with just one byte per character. For that reason it is not advisable to directly output all data into one text file because of that would result in a text file using more than one character encoding making the file unreadable. Therefore each data output is first written into a temporary file always created new. The temporary file content is output next with command TYPE and the one byte per character encoded output is appended to the record file.
Note 3:
It is necessary to escape both % around Microsoft Office with three additional percent signs to pass %Microsoft Office% to wmic.exe executed in subroutine OutputData. Each % must be escaped with one more % to be interpreted as literal character in a batch file. But a command line with CALL is processed a second time by the Windows command processor. Therefore two more percent signs are necessary on both sides to get first %%Microsoft Office%% after first parsing of the command line and next %Microsoft Office% after the second parsing caused by command CALL.
Note 4:
The usage of just %Office% instead of %Microsoft Office% could result in output not really wanted like:
0 Microsoft Visual Studio 2010 Tools for Office Runtime (x64) 10.0.50908
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
cmd /?
del /?
echo /?
exit /?
for /?
goto /?
if /?
pause /?
powershell get-help get-physicaldisk
set /?
start /?
systeminfo /?
title /?
type /?
wmic /?
wmic cpu /?
wmic cpu get /? and Win32_Processor class
wmic memorychip /?
wmic memorychip get /? and Win32_PhysicalMemory class
wmic product /?
wmic product get /? and Win32_Product class
See also:
Microsoft documentation about Using command redirection operators
DosTips forum topic: ECHO. FAILS to give text or blank line - Instead use ECHO/
How does the Windows Command Interpreter (CMD.EXE) parse scripts?
Single line with multiple commands using Windows batch file
There are very efficient small utilities such as windows versions of Tee or Tail that can help greatly in such situations. However, in the spirit of going commando one possibility is to wrap your existing batch file in a Power Shell emulation of Tee.
On Windows 7 I had a slight hiccup with your cmd file as get-physicaldisk is not recognized but it did not stall the output too much. See #mofi s Note 1.
Also note your end pause was not visible (due to this method of nesting) so I found replacing pause with
echo Press any key to exit & Pause>nul
worked better for me. Also your max option seems to be backwards since without max I get a larger output!
Anyway, assuming your cmd is "monitor.cmd" the following worked as slowly as expected.
powershell.exe -C "& {cmd /c 'monitor max' | tee -filepath monitor.log}"
to review output you can use
type monitor.log
I'm trying to create a basic script to uninstall an application across all our endpoints using cmd;
msiexec /quiet /norestart /uninstall {xxxx-xxx-xxx-xxxx-xxxxx}
Due to different versions, the same app may have multiple GUIDs on different endpoints
How can I run the wmic product get name,IdentifyingNumber cmdlet to search for a specific application and set it's GUID also to a variable?
wmic product get name,IdentifyingNumber"
IdentifyingNumber Name
{E8CAD3B5-7016-45AE-97DF-098B5C8D4AC8} App1
{90160000-008C-0000-1000-0000000FF1CE} App
I can find and match the Application to a variable, bit is struggling setting the GUID to a variable.
FOR /F "tokens=2 delims==" %%A IN ('WMIC product GET Name /VALUE ^| FIND /I "App1"') DO SET _application=%%A
ECHO Application: "%_application%"
Rem ECHO GUID:%_GUIDVALUE% //Matching Application GUID
Any help would appreciated
wmic has a few ugly habits that makes its output hard to parse *), but it's not impossible:
#echo off
setlocal
for /f %%a in ('wmic product where "name='Update for Windows 10 for x64-based Systems (KB4023057)'" get name^,IdentifyingNumber^|find "{"') do set "GUID=%%a"
echo %GUID%
*) especially
1. an ugly line ending of CRCRLF, which doesn't apply here, because we don't use a string "at the end of a line".
2. Some "empty" lines (not really empty - they contain a remaining CR), which we overcome with find "{" (we know, that char will be in the desired line).
A FOR /F loop can retrieve the GUID ID. Change the value of APPNAME to what you are seeking. How are you planning to hadle the case of multiple versions of an app being installed on the machine?
#ECHO OFF
SET "APPNAME=Windows SDK"
FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command ^
"Get-CimInstance -Class Win32_Product |" ^
"Where-Object { $_.Name -eq '%APPNAME%' } |" ^
"ForEach-Object { $_.IdentifyingNumber }"') DO (SET "APPID=%%A")
ECHO APPID is %APPID%
I have a deployment that installs a driver and I want to provide the ability to uninstall.
Im leveraging the PNPUTIL.exe tool.
I know the syntax to delete and uninstall the driver, ex:
pnputil.exe /delete-driver oem103.inf /uninstall /force
But my issue, is the oem*.inf number designation is random on each machine, so I can't hard code the .inf into the command and call it a day.
pnputil has /enum-driver switch that will give you details of all the drivers in the DriverStore. Among the line items is the original name of .inf (something I can work with) and the oem# associated with it.
So what I need help with is scripting something that will enumerate the drivers pipe the results to the command to be able the run /delete-drive and /uninstall switches
I tried messing with the Find and FindSTR commands, but it only returned the one line which was the name of the original .inf. I need the OEM# associated with original name of the .inf to be piped to the command.
In the output of pnputil, the desired oemXX.inf is one line above the Original Name.
So numerate the output, look for the original name and subtract one from the line number. This is the line number where you find the oemXX.inf.
Then find that line and extract the oemXX string. (the for %%b is to get rid of the leading spaces)
#echo off
setlocal
set "Orig=rt640x64.inf"
pnputil /enum-drivers |findstr /n "^" > pnputil.txt
for /f "delims=:" %%a in ('findstr /c:" %Orig%" pnputil.txt') do set /a line=%%a-1
for /f "tokens=3 delims=:" %%a in ('findstr /b "%line%:" pnputil.txt') do for %%b in (%%a) do set "oem=%%b"
echo "%oem%"
Note: the output of pnputil is language-dependent, but this code doesn't look for words (except the "Original name" of course) but for line numbers, so it should work on all languages.
I have a XML file generated by Visual Studio that contains the version of the product, hence the tag will always be there just the value changes.
I need to extract the version with windows command line somehow. This is the tag in the file and I need only the version number "1.0.0.0":
<?define BuildVersion = 1.0.0.0 ?>
Is there a built in tool (maybe with findstr) that can accomplish this?
Use for /F loop to parse the output of findstr and extract the text you need (using space as delimiter, get the 4th token):
for /F "tokens=4 delims= " %%E in ('
findstr "BuildVersion" "file.xml"
') do #echo %%E
Use a single % instead of two when running from the command line though.
For a more comprehensive information please check this post.
I have a file with Version resource that File vesrion/Product version fields are filled. I need to retrieve Product version via BAT file. Example, I have File with ProductVersion 1.0.1 in the output of bat file I wan't to have string "101" or "1.0.1"
You can use sigcheck tool which is part of Sysinternals Suite since filever is quiet old, e.g.
$ sigcheck.exe -q -n app.exe
5.0.0.1241
By specifying -q (quiet) and -n, it'll show you only the file version number.
How to use the Filever.exe tool to obtain specific information about a file in Windows
From what I gather about filever's output it's always in columns and you want the fifth column (version). So a simple for should suffice:
for /f "tokens=5 delims= " %%v in ('filever myFile.dll /b') do echo %%v
For dummy's like me there is one correction in the above statement to get the value of product version it would be like:
for /f "tokens=5 delims= " %%v in ('filever myFile.dll /b /v') do echo %%v
the /v parameter was missing and I am unable to get the correct value.
To read version from resource RC-file you can use:
for /F "tokens=3" %%a in ( 'findstr ProductVersion YourProgram.rc' ) do set VERSION=%%~a