Code: Select all
#SingleInstance, Force
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
; Function to get total physical memory using Get-CimInstance
GetTotalPhysicalMemory()
{
; Define the PowerShell command
psCommand := "powershell -command ""& { Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object -ExpandProperty TotalPhysicalMemory }"""
; Run the PowerShell command and copy the result to the clipboard
RunWait, %ComSpec% /c %psCommand% | clip, , Hide
; Retrieve the result from the clipboard
ClipWait, 2
totalMemory := Clipboard
; Convert the result from bytes to megabytes
totalMemoryMB := Round(totalMemory / 1MB, 2)
MsgBox, %totalMemory% ; This shows 8464130048 (bytes) correctly
MsgBox, %totalMemoryMB% ; This shows an incorrect result of 0.00... I was expecting 8.00
return totalMemoryMB
}
; Example usage of the function
totalMemoryMB := GetTotalPhysicalMemory()
MsgBox, Total Physical Memory: %totalMemoryMB% MB
GuiClose:
ExitApp
In the code above I can use PowerShell 5.1 to query the amount of RAM and send the result to the Windows clipboard. Both CTRL+V and MsgBox show the amount of RAM in bytes correctly.
Next I try to display the amount in MB, rounded to 2 decimal places. I'm expecting to see 8.00 MB.. but it shows 0.00 MB every time. I don't understand why the expression totalMemoryMB := Round(totalMemory / 1MB, 2) is not being evaluated correctly
Can anyone see and explain what I'm doing wrong?