Public/Get-MemoryUsage.ps1

function Get-MemoryUsage {
    <#
    .SYNOPSIS
        Returns current physical memory usage.
 
    .DESCRIPTION
        Wraps Get-CimInstance Win32_OperatingSystem to report total,
        used, and free physical memory, plus percent used.
        Windows-only.
 
    .EXAMPLE
        Get-MemoryUsage
 
        Returns a single object with memory usage stats.
    #>

    [CmdletBinding()]
    param ()

    if (-not $IsWindows -and $PSVersionTable.PSEdition -eq 'Core') {
        throw "Get-MemoryUsage currently supports Windows only."
    }

    $os = Get-CimInstance -ClassName Win32_OperatingSystem

    $totalKB = $os.TotalVisibleMemorySize
    $freeKB  = $os.FreePhysicalMemory
    $usedKB  = $totalKB - $freeKB

    [PSCustomObject]@{
        TotalMemory   = ConvertTo-FriendlySize -KB $totalKB
        UsedMemory    = ConvertTo-FriendlySize -KB $usedKB
        FreeMemory    = ConvertTo-FriendlySize -KB $freeKB
        PercentUsed   = [math]::Round(($usedKB / $totalKB) * 100, 1)
    }
}