I'm working on a batch file to extract computer infos from systeminfo command.
The first version of my script (it's a french version of Windows 11 Entreprise) :
for /f "tokens=5,* delims= " %%a in ('systeminfo ^| find ^"Nom du syst^"') do set osname=%%a %%b
for /f "tokens=4,* delims= " %%a in ('systeminfo ^| find ^"Date d'installation^"') do set osinstall=%%a %%b
for /f "tokens=6,* delims= " %%a in ('systeminfo ^| find ^"Heure de d^"') do set lastboot=%%a %%b
for /f "tokens=4" %%a in ('systeminfo ^| find ^"physique totale^"') do set totalmem=%%a
for /f "tokens=4" %%a in ('systeminfo ^| find ^"physique disponible^"') do set freemem=%%a
It works well, but it is slow because each line lauches systeminfo again ...
Then i have found that one :
for /f "tokens=*" %%a in ('systeminfo ^| findstr /B /C:"Nom du syst" /C:"Date d'installation" /C:"Heure de d" /C:"physique totale" /C:"physique disponible"') do @echo %%a
It also works well, but the layout of the result does not fit my needs. I would like to have each info in separate variables, to make a custom layout.
Then, i've tested :
systeminfo >systeminfo.txt
With :
for /f "tokens=5,* delims= " %%a in ('find ^"Nom du syst^" systeminfo.txt') do set osname=%%a %%b
for /f "tokens=4,* delims= " %%a in ('find ^"installation^" systeminfo.txt') do set osinstall=%%a %%b
for /f "tokens=6,* delims= " %%a in ('find ^"Heure de démarrage^" systeminfo.txt') do set lastboot=%%a %%b
for /f "tokens=4" %%a in ('find ^"physique totale^" systeminfo.txt') do set totalmem=%%a
for /f "tokens=4" %%a in ('find ^"physique disponible^" systeminfo.txt') do set freemem=%%a
It also worked well.
Then, i wanted to avoid the systeminfo.txt, but did not find a solution.
Is it possible to put the systeminfo result in a variable, and then use the For /F command to search strings in that variable ?
The first part would be something like this :
for /f "tokens=*" %%a in ('systeminfo ^| findstr /B /C:"Nom du syst" /C:"Date d'installation" /C:"Heure de d" /C:"physique totale" /C:"physique disponible"') do set informations=%%a
Then something like this :
for /f "tokens=5,* delims= " %%a in ('find ^"Nom du syst^" %informations%') do set osname=%%a %%b
for /f "tokens=4,* delims= " %%a in ('find ^"installation^" %informations%') do set osinstall=%%a %%b
for /f "tokens=6,* delims= " %%a in ('find ^"Heure de démarrage^" %informations%') do set lastboot=%%a %%b
for /f "tokens=4" %%a in ('find ^"physique totale^" %informations%') do set totalmem=%%a
for /f "tokens=4" %%a in ('find ^"physique disponible^" %informations%') do set freemem=%%a
I've tryied to put systeminfo in a variable, then used for / f command, but i have a message telling me the file was not found ...
Thank you for your time.
---------- Edit 01 (answer to MOFI) : ----------
That's the result of :
echo off
cls
echo.
systeminfo | findstr /B /C:"Nom du syst" /C:"Date d'installation" /C:"Heure de d" /C:"physique totale" /C:"physique disponible"
echo.
pause
And that's the output i have using the batch with systeminfo.txt with that script :
echo off
cls
chcp 65001>nul
mode con:cols=120 lines=44
reg.exe ADD "HKCU\Software\Sysinternals\PsExec" /v EulaAccepted /t REG_DWORD /d 1 /f
reg.exe ADD "HKCU\Software\Sysinternals\PsGetsid" /v EulaAccepted /t REG_DWORD /d 1 /f
reg.exe ADD "HKCU\Software\Sysinternals\PsInfo" /v EulaAccepted /t REG_DWORD /d 1 /f
reg.exe ADD "HKCU\Software\Sysinternals\PsLoggedon" /v EulaAccepted /t REG_DWORD /d 1 /f
cls
echo.
systeminfo >systeminfo.txt
cls
for /f "tokens=5,* delims= " %%a in ('find ^"Nom du syst^" systeminfo.txt') do set osname=%%a %%b
for /f "tokens=4,* delims= " %%a in ('find ^"installation^" systeminfo.txt') do set osinstall=%%a %%b
for /f "tokens=6,* delims= " %%a in ('find ^"Heure de démarrage^" systeminfo.txt') do set lastboot=%%a %%b
cls
for /f "tokens=3" %%a in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" /V "ComputerName"') do set psname=%%a
for /f "tokens=3" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "CurrentVersion"') do set veros=%%a
for /f "tokens=3" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "DisplayVersion"') do set dispver=%%a
for /f "tokens=3" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "CurrentBuild"') do set build=%%a
for /f "usebackq delims== skip=1" %%a in (`.\PsGetsid64 \\%psname%`) do set computerid=%%a
for /f "tokens=4" %%a in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" /v "NV Domain"') do set domaine=%%a
for /f "tokens=3" %%a in ('reg query "HKEY_CURRENT_USER\Volatile Environment" /V "LOGONSERVER"') do set srvauth=%%a
cls
for /f "tokens=3* delims= " %%a in ('reg query "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\BIOS" /v "SystemManufacturer"') do set mannme=%%a %%b
for /f "tokens=3* delims= " %%a in ('reg query "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\BIOS" /v "BaseBoardProduct"') do set mbmodel=%%a
for /f "tokens=3* delims= " %%a in ('reg query "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\BIOS" /v "BaseBoardManufacturer"') do set mbmanufacturer=%%a %%b
for /f "tokens=3* delims= " %%a in ('reg query "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0" /v "ProcessorNameString"') do set cpu=%%a %%b
for /f "usebackq tokens=3,*" %%a in (`.\PsInfo64 \\%psname% ^| find ^"Processor speed^"`) do set cpuspeed=%%a %%b
cls
echo.
for /f "tokens=4" %%a in ('find ^"physique totale^" systeminfo.txt') do set totalmem=%%a
for /f "tokens=4" %%a in ('find ^"physique disponible^" systeminfo.txt') do set freemem=%%a
cls
del systeminfo.txt
cls
echo.
echo Configuration du poste :
echo.
echo.
echo Nom de l'ordinateur : %psname%
echo.
echo Version de Windows : %osname% (%veros%) - %dispver% - build %build%
echo.
echo Date d'installation : %osinstall%
echo.
echo Heure du dernier démarrage : %lastboot%
echo.
echo.
echo Computer ID : %computerid%
echo.
echo Domaine : %domaine%
echo.
echo Serveur d'authentification : %srvauth%
echo.
echo.
echo Marque de l'ordinateur : %mannme%
echo.
echo Carte mere : %mbmodel% - %mbmanufacturer%
echo.
echo Processeur : %cpu% %cpuspeed%
echo.
psinfo64 -d "Volume Type"
echo.
pause
There are lot more infos. Most of them are found in the registry, but some of them are found with sysinternals softs and other with systeminfo only.
---------- Edit 02 (answer to Aacini) : ----------
@Aacini
Here is your script i've tested (i've added "chcp 65001>nul" for the accent we have in french, and modified the first "token" from 1,2 to 1,*
@echo off
chcp 65001>nul
setlocal EnableDelayedExpansion
for /F "tokens=1,* delims=:" %%a in ('systeminfo 2^>NUL') do (
if "%%b" neq "" (
set "var=%%a"
for /F "tokens=*" %%v in ("%%b") do set "!var: =_!=%%v"
)
)
cls
echo.
echo Nom de l'hôte : %Nom_de_l_'hôte%
echo.
echo Nom du système d'exploitation : %Nom_du_système_d'exploitation%
echo.
echo Version du système : %Version_du_système%
echo.
pause
That's the output of that code :
As you see, only "Version du système" gives a result.
---------- Edit 03 (answer to Aacini) : ----------
Echo variable - UTF-8 - no chcp
Output - UTF-8 - no chcp
And
Echo variable - UTF-8 - chcp 65001
Output - UTF-8 - chcp 65001
I would like to present you a very simple solution:
@echo off
setlocal EnableDelayedExpansion
for /F "tokens=1,2 delims=:" %%a in ('systeminfo 2^>NUL') do (
if "%%b" neq "" (
set "var=%%a"
for /F "tokens=*" %%v in ("%%b") do set "!var: =_!=%%v"
)
)
This code takes all the information provided by sysinfo
command and store they in same named variables!!! (with the spaces changed by underscores)
This means that after that code, you can show the value of any variable you want. For example:
echo Nom du syst=%Nom_du_syst%
echo Date d'installation=%Date_d'installation%
Or extract/manipulate such an information in any way you wish. For example:
for /f "tokens=2,* delims= " %%a in ("%Nom_du_syst%") do set osname=%%a %%b
That is it!