Powershell Script Physical Memory

Balayuvaraj M 56 Reputation points
2023-11-15T14:26:36.7266667+00:00

Hello,

Need a powershell script for available physical memory and free physical memory for multiple server

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
13,402 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,690 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 47,466 Reputation points
    2023-11-15T19:54:42.1666667+00:00

    Here's an easy way to get those values:

    $computers = @(".")
    Foreach ($computer in $computers){
        [PSCustomObject]@{
            'PhysicalMemory (bytes)' = ((((Get-CimInstance Win32_PhysicalMemory -ComputerName $computer).Capacity) | Measure-Object -Sum).Sum) # bytes
            'FreePhysicalMemory  (bytes)' = (((Get-CIMInstance Win32_OperatingSystem -ComputerName $computer).FreePhysicalMemory) * 1024)   # convert kilobytes to bytes
        }
    }
    

    If you have a very large number of machines to query this doesn't offer possibility of performing the work in parallel, so it may not be what you need.

    For parallelism you could package the two Get-CimObject cmdlets in a scriptblock and use the scriptblock in an Invoke-Command with the $computers list as the value for -ComputerName. That will return an array of PSCustomObjects. But it does complicate processing because there may be machines that are not available and those would result in ErrorRecord objects in the results.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.