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

后端 未结 8 1779
暖寄归人
暖寄归人 2020-12-31 04:12

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 s

相关标签:
8条回答
  • 2020-12-31 04:29

    This gives you the total amount from another WMI class:

    $cs = get-wmiobject -class "Win32_ComputerSystem"
    $Mem = [math]::Ceiling($cs.TotalPhysicalMemory / 1024 / 1024 / 1024)
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-31 04:29

    Maybe not the best solution, but it worked for me.

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")
    $VBObject=[Microsoft.VisualBasic.Devices.ComputerInfo]::new()
    $SystemMemory=$VBObject.TotalPhysicalMemory
    
    0 讨论(0)
  • 2020-12-31 04:34

    Let's not over complicate things...:

    (Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property capacity -Sum).sum /1gb
    
    0 讨论(0)
  • 2020-12-31 04:38

    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}
    
    0 讨论(0)
  • 2020-12-31 04:45

    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
    
    0 讨论(0)
  • 2020-12-31 04:46

    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

    0 讨论(0)
提交回复
热议问题