Sub PhysicalMemWMI()
Dim dTotalMemory As Double
Dim dAvailable As Double
Dim dFreeMem As Double
Dim strComputer As String
Dim oWMISrvEx As Object
Dim oWMIObjSet As Object
Dim oWMIObjEx As Object
Dim colItems As Object
Dim objItem As Object
Dim sWQL As String
Dim rPaste As Range
strComputer = InputBox(prompt:="Enter computer name (use . for the local computer)", Title:="Computer name", Default:=".")
If StrPtr(strComputer) = 0 Then Exit Sub
On Error Resume Next
Set rPaste = Application.InputBox(prompt:="Select range to output data to", Title:="Output range", Type:=8)
On Error GoTo 0
If rPaste Is Nothing Then Exit Sub
sWQL = "SELECT * FROM Win32_OperatingSystem"
Set oWMISrvEx = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set oWMIObjSet = oWMISrvEx.ExecQuery(sWQL)
With rPaste.Cells(1).Resize(1, 3)
.Value = Array("TotalVisibleMemorySize", "FreePhysicalMemory", "In use")
.Font.Bold = True
End With
For Each oWMIObjEx In oWMIObjSet
dTotalMemory = dTotalMemory + oWMIObjEx.TotalVisibleMemorySize
Next
dTotalMemory = dTotalMemory / 1024
Set colItems = oWMISrvEx.ExecQuery("Select * from Win32_PerfFormattedData_PerfOS_Memory", , 48)
For Each objItem In colItems
dFreeMem = dFreeMem + objItem.FreeAndZeroPageListBytes
dAvailable = dAvailable + objItem.AvailableBytes
Next objItem
dFreeMem = dFreeMem / 1024 / 1024
rPaste.Cells(1).Offset(1).Resize(1, 3).Value2 = Array(Format(dTotalMemory, "#,##0 MB"), Format(dFreeMem, "#,##0 MB"), Format(((dTotalMemory * 1024 * 1024) - dAvailable) / 1024 / 1024 / 1024, "#,##0.00 GB"))
End Sub