Powershell – Check Free Memory script

 

Here’s a quick script I did using Powershell to check your free memory and report back the amount in MB and GB.

 

$freemem = Get-WmiObject -Class Win32_OperatingSystem

# Display free memory on PC/Server
"---------FREE MEMORY CHECK----------"
""
"System Name     : {0}" -f $freemem.csname
"Free Memory (MB): {0}" -f ([math]::round($freemem.FreePhysicalMemory / 1024, 2))
"Free Memory (GB): {0}" -f ([math]::round(($freemem.FreePhysicalMemory / 1024 / 1024), 2))
""
"------------------------------------"

Download the script here

 

The figure is determined and held in the $freemem variable. After that we simply output two lines to show the amount in MB and GB. We use a simple function to divide the figure by 1024 and round it off, displaying the result with two decimal places. The figure needs to be divided by 1024 as the variable holds the amount in Kilobytes (KB), therefore to determine Megabytes (MB), we divide by 1024. The second figure for GB requires one more division.

 

 

 

2 thoughts on “Powershell – Check Free Memory script”

  1. @Abid

    Hi Abid,

    Pretty simple, you can just use the Add-Content PowerShell cmdlet. Like so:

    $freemem = Get-WmiObject -Class Win32_OperatingSystem

    # Display free memory on PC/Server
    $SystemName = “System name: ” + $freemem.csname
    $FreeMemoryMB = “Free Memory MB: ” + ([math]::round($freemem.FreePhysicalMemory / 1024, 2))
    $FreeMemoryGB = “Free Memory GB: ” + ([math]::round(($freemem.FreePhysicalMemory / 1024 / 1024), 2))

    Add-Content C:\temp\mydata.txt $SystemName
    Add-Content C:\temp\mydata.txt $FreeMemoryMB
    Add-Content C:\temp\mydata.txt $FreeMemoryGB

  2. Hi,
    Thanks for above script.

    If I want to log this data to a txt file with date and time. How i can do it ? please help

    Regards,
    Abid

Leave a Comment