I have a script that works perfectly.
if not exist %SystemRoot%\system32\systeminfo.exe goto warnthenexit
systeminfo | find "OS Name" > osname.txt
FOR /F "usebackq delims=: tokens=2" %%i IN (osname.txt) DO set vers=%%i
echo %vers% | find "Windows 7" > nul
if %ERRORLEVEL% == 0 goto VERSION_7
However, when I try to print the results like so:
Echo Current OS:%vers% - 64 Bit
It prints the results with a really! long gap. Like so:
Current OS: Microsoft Windows 7 Enterprise - 64 Bit
Does anyone know why? or how I can remove the large gap?
Thanks a million!
I know your script "works perfectly", but is not much efficient.
SYSTEMINFO takes a lot of time and osname.txt may be avoided.
Check:
FOR /F "TOKENS=1,* DELIMS==" %%u IN ('WMIC OS GET CAPTION /VALUE') DO IF /I "%%u"=="Caption" SET vers=%%v
Changed
FOR /F "usebackq delims=: tokens=2" %%i IN (osname.txt) DO set vers=%%i
To
FOR /F "usebackq delims= tokens=2" %%i IN (osname.txt) DO set vers=%%i
*Changed the : to a %space% after delims...
Related
I found this post with regard to getting a list of installed Hotfixes using wmic qfe list full and #Hackoo replied with the following:
#echo off
Title wmic to get HotfixID
Setlocal EnableDelayedExpansion
echo "patches" : {
set "patches=wmic qfe get HotfixID"
for /f "skip=1" %%i in ('%patches%') do for /f "delims=" %%j in ("%%i") do (
set /a count=count+1
echo "!count!" : "%%j",
)
echo }
This works absolutely fine but is it also possible to incorporate the Description and InstalledOn wmic information as well so that the output displays the following:
HotfixID InstalledOn Description
Using the above code I can get each individually but not all together as the InstalledOn / Description seem to repeat the first value.
I then stepped right outside my level of knowledge and tried the following (which does not work):
for /f "tokens=1,2,3 skip=1 delims=," %%a in ('%SystemRoot%\System32\wbem\wmic.exe qfe get HotfixID,InstalledOn,Description') do (
set "hotfix_number=%%~a"
set "hotfix_installed=%%~b"
set "hotfix_description=%%~c"
)
echo %hotfix_number% installed on %hotfix_installed% - %hotfix_description%
Here's hoping you are able to assist.
Does this help?
#For /F "Skip=2 Tokens=1,* Delims=," %%G In ('%SystemRoot%\System32\wbem\WMIC.exe QFE Get Description^, HotFixID^, InstalledOn /Format:CSV 2^>NUL') Do #For /F "Tokens=1-3 Delims=," %%I In ("%%H") Do #Echo %%J installed on %%K - %%I
I am trying to get the current operating system using this batch file. It is:
#echo off
for /f "skip=1 tokens=1* delims= " %%a in ('wmic path win32_operatingsystem get caption') do set _os=%%a %%b %%c %%d
echo You are running %_os%.
goto :eof
My operating system is windows 7 so I expected it will return:
You are running Microsoft Windows 7 Ultimate.
But it returns you are running %c %d.
Why I am getting this result?
There are a few ways, one already posted by Mofi in the above comment which already demonstrates the main change using /value. You can also use the caption itself as variable name with its value:
#echo off
for /f "delims=" %%a in ('wmic path win32_operatingsystem get caption /value') do set %%a>nul 2>&1
echo %caption%
While scripting it is necessary to determine what version of Windows a computer is running. The only issue is that Windows-10 and Server-2016 have the same version number.
This is the code currently. In this state, Windows10 and Server2016 are identified with the same number wmic os get outputs 10.0.18329 (which doesn't work).
for /f "tokens=4-5 delims=. " %%i in ('ver') do set Operating=%%i.%%j
if "%Operating%" == "6.3" set Operating=Windows81
if "%Operating%" == "6.2" set Operating=Windows8
if "%Operating%" == "10.0" set Operating=Windows10
if "%Operating%" == "10.0" set Operating=Server2016
echo Is %Operating% your operating system?
What I would like to do is parsewmic os get name to get the OS name. This is showing some challenges because the output is multiple lines and there is an arbitrary number of characters before and after the OS name ('Windows 10').
This is the output of wmic os get name on windows 10 pro:
Name
Microsoft Windows 10 Pro|C:\WINDOWS|\Device\Harddisk1\Partition3
This is the output of wmic os get name on Server 2016:
Name
Microsoft Windows Server 2016 Standard|C:\Windows|\Device\Harddisk0\Partition2
I know you've already chosen a solution, but if this is related to the last question you asked, now deleted, where you're only trying to determine between Windows 8, Windows 8.1, Windows 10 and Windows Server 2016 then I would have suggested something along these lines:
#Echo Off
Set "_S="
For /F "EOL=P Tokens=*" %%A In ('"WMIC OS Get ProductType,Version 2>Nul"'
) Do For /F "Tokens=1-3 Delims=. " %%B In ("%%A") Do Set /A _S=%%B,_V=%%C%%D
If Not Defined _S Exit /B
If %_V% Lss 62 Exit /B
If %_S% Equ 1 (If %_V% Equ 62 Set "_S=Windows8"
If %_V% Equ 63 Set "_S=Windows81"
If %_V% Equ 100 Set "_S=Windows10"
) Else If %_V% Equ 100 (Set "_S=Server2016") Else Exit /B
Set _S
Pause
Exit /B
[Edit /]
…and for a generic method, you may find this useful, especially the use of the Caption property, (which I'd suggest is much better than using the Name property, splitting it at the first pipe character, running it through another For loop then removing one or more trailing space characters).:
#For /F Tokens^=6Delims^=^" %%A In ('WMIC OS Get Caption/Format:MOF')Do Set "_S=%%A"
To test it just add a second line:
#Set _S&Pause
Note: This method may have issues on some earlier operating systems due to a bug preventing them finding the location of the .xsl file. There are workarounds for this, either copy the .xsl files from your 'language' directory up a level, or provide the full path to the .xsl file, e.g. /Format:"%__AppDir__%wbem\en-US\MOF.xsl".
How about just straight forward...
#echo off
for /f "skip=1 delims=|" %%i in ('wmic os get name') do echo %%i & exit /b
This is simple:
#echo off
for /F "skip=1 tokens=2-4 delims=| " %%A IN ('wmic os get name') do (
set "os=%%A %%B %%C"
)
echo You are using %os%.
Another possible solution to include all versions of Windows:
#echo off
for /F "skip=1 delims=|" %%A IN ('wmic os get name') do (
for /F "delims=" %%B IN ("%%A") do (
set "os=%%B"
)
)
echo You are using %os:Microsoft =%.
pause
exit /b
Which will extract OS, substracting Microsoft<space> at the end in both cases.
In the second case, two loops are used, since wmic has unusual line endings (<CR><CR><LF>).
You can use the ver command, here is an example:
#echo off
for /f "useback delimes=;" %%a in (`ver`) do set ver=%%a
echo The computer version your running is %ver%
Use the variable %ver% as the version you are running, I use it to echo the version but you can do anything with it
Hello dear people and others,
Today i wanted to create a simple script, thought it would be easy to store the outcome to var of the following command:
wmic bios get serialnumber | findstr /N /V SerialNumber
Outcome:
2:H3GK4S1
3:
The problem is when i try to get the serial with wmic, it returns the string as expected but also an empty string/line. When i try to store the serial to a variable it stores it and then directly overwrites it with the empty string. This is the function i nearly got working now:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber ^| FINDSTR /N /V SerialNumber') DO (SET serial=%g & ECHO %g)
And this gives the following output:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber ^| FINDSTR /N /V SerialNumber') DO (SET serial=%g & ECHO %g)
2:H3GK4S1
3:
As can be seen above, the loop overwrites the serial var, if someone can help me towards the right directon to get this working, would be mad.
At the Command Prompt:
For /F "Tokens=1* Delims==" %g In ('WMIC BIOS Get SerialNumber /Value') Do #For /F "Tokens=*" %i In ("%h") Do #Set "serial=%i" & Echo %i
Or in a batch file:
#For /F "Tokens=1* Delims==" %%g In ('WMIC BIOS Get SerialNumber /Value'
) Do #For /F "Tokens=*" %%i In ("%%h") Do #Set "serial=%%i" & Echo %%i
#Pause
EditIf you're happy to use a labelled section in your batch file:
#Echo Off
Set "serial="
For /F "Skip=1 Delims=" %%A In ('WMIC BIOS Get SerialNumber') Do If Not Defined serial Call :Sub %%A
Set serial 2>Nul
Pause
GoTo :EOF
:Sub
Set "serial=%*"
GoTo :EOF
Try like this:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber /format:value') DO for /f "tokens=* delims=" %# in ("%g") do set "serial=%#"
echo %serial%
Mind that's a command that should be executed in the command prompt directly.For a batch file you'll need to double the % in the for loop tokens.
In a batch file, you can also use a goto to end the loop after the first iteration :
#echo off
for /f "tokens=2 delims=:" %%a in ('wmic bios get serialnumber ^| findstr /N /V SerialNumber') do (
set "$var=%%a"
goto:next
)
exit/b
:next
echo Result=^> [%$var: =%]
At the moment I am running the following to get some information from 'systeminfo', however it needs to run systeminfo twice which takes some time. How would I be able to do multple 'Find"XXX" do sets'?
For /f "delims=" %%A IN ('systeminfo ^| Find "OS Name"') DO Set "VarA=%%A"
For /f "delims=" %%A IN ('systeminfo ^| Find "BIOS Version"') DO Set "VarB=%%A"
Any help is greatly appreciated.
Next code snippet could work (note set commands are merely ECHOed for debugging purposes; remove capitalized ECHO no sooner than debugged):
For /f "delims=" %%A IN ('systeminfo') DO (
For /F "delims=" %%G IN ('echo %%A ^| Find /I "OS Name"') Do ECHO Set "VarA=%%A"
For /F "delims=" %%G IN ('echo %%A ^| Find /I "BIOS Version"') DO ECHO Set "VarB=%%A"
)
However, follow Stephan's advice and parse wmic output rather (do not forget /value option). To do that correctly, note great Dave Benham's article WMIC and FOR /F: A fix for the trailing <CR> problem
I too would use WMIC, but there is a simple solution if you want to use SYSTEMINFO - use FINDSTR with two /C:"search strings"
for /f "tokens=1,2*" %A in ('systeminfo ^| findstr /c:"OS Name" /c:"BIOS Version"') do set "%A=%C"
The above will define two variables on an English machine: OS and BIOS.
Thanks for the replies guys.
I wanted to set the bit version windows the batch file would run on, I figured it out ages ago but thought I would post it here for those that came across it:
echo %PROCESSOR_ARCHITECTURE% | find "64" > NUL
If %ERRORLEVEL% equ 0 (Set bit=64) else (Set bit=32)
Thanks!