How do I get total physical memory size using PowerShell without WMI?

142,635

Solution 1

If you don't want to use WMI, I can suggest systeminfo.exe. But, there may be a better way to do that.

(systeminfo | Select-String 'Total Physical Memory:').ToString().Split(':')[1].Trim()

Solution 2

Let's not over complicate things...:

(Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property capacity -Sum).sum /1gb

Solution 3

I'd like to make a note of this for people referencing in the future.

I wanted to avoid WMI because it uses a DCOM protocol, requiring the remote computer to have the necessary permissions, which could only be setup manually on that remote computer.

So, I wanted to avoid using WMI, but using get-counter often times didn't have the performance counter I wanted.

The solution I used was the Common Information Model (CIM). Unlike WMI, CIM doesn't use DCOM by default. Instead of returning WMI objects, CIM cmdlets return PowerShell objects.

CIM uses the Ws-MAN protocol by default, but it only works with computers that have access to Ws-Man 3.0 or later. So, earlier versions of PowerShell wouldn't be able to issue CIM cmdlets.

The cmdlet I ended up using to get total physical memory size was:

get-ciminstance -class "cim_physicalmemory" | % {$_.Capacity}

Solution 4

Id like to say that instead of going with the systeminfo this would help over to get the total physical memory in GB's the machine

Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property capacity -Sum | Foreach {"{0:N2}" -f ([math]::round(($_.Sum / 1GB),2))}

you can pass this value to the variable and get the gross output for the total physical memory in the machine

   $totalmemory = Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property capacity -Sum | Foreach {"{0:N2}" -f ([math]::round(($_.Sum / 1GB),2))}
   $totalmemory

Solution 5

For those coming here from a later day and age and one a working solution:

(Get-WmiObject -class "cim_physicalmemory" | Measure-Object -Property Capacity -Sum).Sum

this will give the total sum of bytes.

$bytes = (Get-WmiObject -class "cim_physicalmemory" | Measure-Object -Property Capacity -Sum).Sum

$kb = $bytes / 1024
$mb = $bytes / 1024 / 1024
$gb = $bytes / 1024 / 1024 / 1024

I tested this up to windows server 2008 (winver 6.0) even there this command seems to work

Share:
142,635

Related videos on Youtube

eltaco431
Author by

eltaco431

I like steak.

Updated on September 23, 2021

Comments

  • eltaco431
    eltaco431 almost 3 years

    I'm trying to get the physical memory size using PowerShell, but without using get-wmiobject.

    I have been using the following PS cmdlet to get the physical memory size, but the value changes with each new poll.

    (get-counter -counter "\Memory\Available Bytes").CounterSamples[0].CookedValue + 
    (get-counter -counter "\Memory\Committed Bytes").CounterSamples[0].CookedValue
    

    In general, this gives me a value around: 8605425664 bytes

    I'm also testing the value I get from adding these counters with the returned value from

    (get-wmiobject -class "win32_physicalmemory" -namespace "root\CIMV2").Capacity
    

    This gives me the value: 8589934592 bytes

    So, not only is the total physical memory calculated from counters changing, but it's value differs from the WMI value by a couple megabytes. Anyone have any ideas as to how to get the physical memory size without using WMI?

    • ravikanth
      ravikanth almost 11 years
      what do you want? Physical Disk or Physical memory? Your example shows physical memory and your question is about physical disk.
    • eltaco431
      eltaco431 almost 11 years
      Sorry. I got them confused. I want physical memory size. I'll edit my question. Thanks!
    • Keith Hill
      Keith Hill almost 11 years
      Just out of curiosity, why do you want to avoid WMI?
    • eltaco431
      eltaco431 almost 11 years
      I'm avoiding WMI because it requires DCOM permissions, so a user without permissions wouldn't be able to get this information.
  • Keith Hill
    Keith Hill almost 11 years
    Interesting, on my system which has 8GB installed (or exactly 8192MB), system info reports that I have a total of 8155MB. I suspect that not all of the 8GB is usable by the system which could account for the difference.
  • ravikanth
    ravikanth almost 11 years
    Yes. Your BIOS may be mapping certain amount of memory for BIOS shadowing functions.
  • Keith Hill
    Keith Hill almost 11 years
    So the question is - what is the OP actually after? Installed memory or total memory available for use by the computer?
  • ravikanth
    ravikanth almost 11 years
    I guess installed memory. Using systeminfo.exe, you cannot actually get the accurate value but that is the only option without WMI, AFAIK.
  • eltaco431
    eltaco431 almost 11 years
    Yes. I'm looking for installed memory. Thank you ravikanth.
  • JensG
    JensG over 8 years
    +1 Slight caveat: does not work on non-english OS, because the output is localized. But that's easy to fix, alternatively (systeminfo | Select-String ' MB')[0] did the trick for me.
  • Dark Daskin
    Dark Daskin almost 8 years
    If the machine has multiple RAM banks installed, your command will return a list. The following one will show a total: (Get-CimInstance -ClassName 'Cim_PhysicalMemory' | Measure-Object -Property Capacity -Sum).Sum
  • Kellen Stuart
    Kellen Stuart about 7 years
    I replaced Get-CimInstance with Get-WmiObject for good measure
  • Cody Barnes
    Cody Barnes over 5 years
    I prefer to use the built in size constants to convert bytes to MB, KB and GB. $kb = $bytes / 1KB $mb = $bytes / 1MB $gb = $bytes / 1GB docs.microsoft.com/en-us/previous-versions/windows/it-pro/…
  • John C
    John C about 5 years
    The KISS principal at is finest!
  • Ricc Babbitt
    Ricc Babbitt over 4 years
    Another Way using syteminfo with the format CSV option and converting to a structured powershell object to get the value systeminfo /FO CSV | ConvertFrom-CSV | select "Total Physical Memory"
  • Piney
    Piney about 3 years
    Great solution TYVM