locked
How to find system memory in Windows PE. RRS feed

  • Question

  • Hi,

    I have written a batch script that runs after WinPE 3.0 loads, which counts the number of POSTs and writes it to a txt file on A:\.

    I have added a few more lines to also read the available memory on each POST and write it to a txt file. The memory test portion works under windows, but not in WinPE. In WinPE the script runs but does not show the memory on command prompt nor does it write it to the txt file. Any suggestions would be appreciated. 

    Thank you

    T.J.

    @echo off
    echo. This script is counting the # of POSTs.
    echo. The POST # value is saved in TEST.txt.
    echo.
                                          
    call :myPOSTTest
    for /f "tokens=* delims=" %%x in (A:\TEST.txt)  do echo POST# %%x 
    echo.&pause&goto:eof
    ::--------------------------------------------------------
    ::-- Function section starts below here
    ::--------------------------------------------------------
    :myPOSTTest    - here starts my function identified by its label  
                              
    set var=0
    
    if EXIST A:\TEST.txt (
         for /f %%x in (A:\TEST.txt) do (set /a var=%%x+1)
    )
    
    echo %var% >> A:\TEST.txt
    ::--------------------------------------------------------
    ::-- Memory report
    ::--------------------------------------------------------
    SETLOCAL
    for /f "tokens=3*" %%I in ('systeminfo 2^>nul^|find "Total Physical Memory"') do set "memory=%%J"
    ECHO from systeminfo: %memory%
    echo %var% >> A:\TESTmem.txt
    echo %memory% >> A:\TESTmem.txt
    for /f "delims=" %%I in ('wmic memphysical^|find "Physical"') do set "memory=%%I"
    FOR %%I IN (%memory:~139%) DO SET memory=%%I&GOTO reportmem
    :reportmem
    ::ECHO from WMIC:%memory%
    
    goto END
    
    :END


    • Edited by TJParsa Wednesday, February 26, 2014 6:37 PM typo
    Wednesday, February 26, 2014 6:17 PM

Answers

  • Alternatively, you can put the VBScript script I posted above into a .vbs file (PHYSMEM.VBS):


    Dim WMI
    Set WMI = GetObject("winmgmts:root/cimv2")
    
    Dim Collection, Item
    Set Collection = WMI.InstancesOf("Win32_ComputerSystem")
    For Each Item In Collection
      WScript.Echo Item.TotalPhysicalMemory
      Exit For
    Next

    You can run the above VBScript script and capture its output in a batch file (shell script), like this:


    @echo off
    setlocal enableextensions
    set COMMAND=%SystemRoot%\system32\cscript.exe //Nologo %~dp0PHYSMEM.VBS
    for /f %%m in ('%COMMAND%') do set MEMORY=%%m
    echo %MEMORY%
    endlocal

    Note that this example assumes PHYSMEM.VBS sits in the same directory as the shell script (batch file).

    Bill

    • Marked as answer by TJParsa Friday, February 28, 2014 9:02 PM
    Friday, February 28, 2014 7:29 PM
  • The way you're approaching it is pretty painful (batch shell script, wmic, string parsing).

    I would recommend asking for the TotalPhysicalMemory property of the Win32_ComputerSystem class. This requires very little code. In PowerShell it's one line:


    (get-wmiobject Win32_ComputerSystem).TotalPhysicalMemory

    In VBScript, it's not much longer:


    Dim WMI
    Set WMI = GetObject("winmgmts:root/cimv2")
    
    Dim Collection, Item
    Set Collection = WMI.InstancesOf("Win32_ComputerSystem")
    For Each Item In Collection
      WScript.Echo Item.TotalPhysicalMemory
      Exit For
    Next
    

    Bill

    • Marked as answer by TJParsa Friday, February 28, 2014 4:48 PM
    Wednesday, February 26, 2014 7:34 PM

All replies

  • What's provoking the question?

    Bill

    Wednesday, February 26, 2014 7:14 PM
  • When I run the same script in windows 7 it runs fine, shows the mount of memory and writes it to the txt file. In Windows PE, the amount of memory shows up blank and the txt file says "ECHO is off" instead of the memory value. Is there WMIC commands that define the memory usage??
    Wednesday, February 26, 2014 7:17 PM
  • Why do you need to know the amount of memory?

    Bill

    Wednesday, February 26, 2014 7:20 PM
  • to make sure the system did not drop memory during the POST tests.

    Thanks for showing interest,

    T.J.

    Wednesday, February 26, 2014 7:23 PM
  • The way you're approaching it is pretty painful (batch shell script, wmic, string parsing).

    I would recommend asking for the TotalPhysicalMemory property of the Win32_ComputerSystem class. This requires very little code. In PowerShell it's one line:


    (get-wmiobject Win32_ComputerSystem).TotalPhysicalMemory

    In VBScript, it's not much longer:


    Dim WMI
    Set WMI = GetObject("winmgmts:root/cimv2")
    
    Dim Collection, Item
    Set Collection = WMI.InstancesOf("Win32_ComputerSystem")
    For Each Item In Collection
      WScript.Echo Item.TotalPhysicalMemory
      Exit For
    Next
    

    Bill

    • Marked as answer by TJParsa Friday, February 28, 2014 4:48 PM
    Wednesday, February 26, 2014 7:34 PM
  • Bill,

    once I run this command in power shell, how would I record the value to the txt file?? 

    (get-wmiobject Win32_ComputerSystem).TotalPhysicalMemory
    Friday, February 28, 2014 4:49 PM

  • (get-wmiobject Win32_ComputerSystem).TotalPhysicalMemory |
      out-file textfilename.txt -encoding ASCII
    

    Bill

    Friday, February 28, 2014 4:56 PM
  • Bill,

    If I just type powershell then 

    (get-wmiobject Win32_ComputerSystem).TotalPhysicalMemory

    the memory size shows up on the screen but when I put in my batch file, the code immediately jump to power shell and doesn't run my POST count. My batch file is suppose to count the number of POSTs and memory on each start up and save them to two txt files, TEST.txt and TESTmem.txt. Any idea as to why the script jumps straight to power shell instead of executing on order???

    @echo off
    echo. This script is counting the # of POSTs.
    echo. The POST # value is saved in TEST.txt.
    echo.
                                          
    call :myPOSTTest
    for /f "tokens=* delims=" %%x in (A:\TEST.txt)  do echo POST# %%x 
    echo.&pause&goto:eof
    ::--------------------------------------------------------
    ::-- Function section starts below here
    ::--------------------------------------------------------
    :myPOSTTest    - here starts my function identified by its label  
                              
    set var=0
    
    if EXIST A:\TEST.txt (
         for /f %%x in (A:\TEST.txt) do (set /a var=%%x+1)
    )
    
    echo %var% >> A:\TEST.txt
    echo %var% >> A:\TESTmem.txt
    call :MemReport
    ::--------------------------------------------------------
    ::-- Memory report
    ::--------------------------------------------------------
    :MemReport
    powershell
    (get-wmiobject Win32_ComputerSystem).TotalPhysicalMemory |
      out-file TESTMem.txt -encoding ASCII
    goto END
    
    :END

    Thanks Bill

    Friday, February 28, 2014 5:17 PM
  • this code 

    (get-wmiobject Win32_ComputerSystem).TotalPhysicalMemory | out-file TESTMem.txt -encoding ASCII

    does not record the value to the txt file either !!

    Friday, February 28, 2014 5:19 PM
  • You can't put a PowerShell command in a batch file.

    I recommend re-writing your batch file (cmd.exe shell script) as a PowerShell script instead.

    Bill

    Friday, February 28, 2014 7:14 PM
  • Alternatively, you can put the VBScript script I posted above into a .vbs file (PHYSMEM.VBS):


    Dim WMI
    Set WMI = GetObject("winmgmts:root/cimv2")
    
    Dim Collection, Item
    Set Collection = WMI.InstancesOf("Win32_ComputerSystem")
    For Each Item In Collection
      WScript.Echo Item.TotalPhysicalMemory
      Exit For
    Next

    You can run the above VBScript script and capture its output in a batch file (shell script), like this:


    @echo off
    setlocal enableextensions
    set COMMAND=%SystemRoot%\system32\cscript.exe //Nologo %~dp0PHYSMEM.VBS
    for /f %%m in ('%COMMAND%') do set MEMORY=%%m
    echo %MEMORY%
    endlocal

    Note that this example assumes PHYSMEM.VBS sits in the same directory as the shell script (batch file).

    Bill

    • Marked as answer by TJParsa Friday, February 28, 2014 9:02 PM
    Friday, February 28, 2014 7:29 PM
  • Mr, Stewart

    I would like to thank you for following up and helping me out.

    T.J.

    Friday, February 28, 2014 9:03 PM