Simple PowerShell Script displays Total and Available Memory .

This script is an update to a previous post. In addition to displaying total and available disk space, it will also show the available memory at any given time for a server within a domain infrastructure.

It has come in handy for me when I need to quickly spin up a standalone virtual machine on a Hyper-v host, by giving me a quick insight into the amount of memory available to the Hyper-v host.

Using the WMIObject win32_operatingsystem class, I can filter out the TotalVisibleMemorySize property, the FreePhysicalMemory and display them to the console to get a quick look at the resources available to me on a stand alone host or server. The script can run in any Windows infrastructure environment with the appropriate user priviledges and firewall rules enabled.


function Get-HostProperties{

param ($computerName = (Read-Host "Enter Server Name")
)

Get-WmiObject -Class win32_logicaldisk -ComputerName $computerName | ft DeviceID, @{Name="Free Disk Space (GB)";e={$_.FreeSpace /1GB}}, @{Name="Total Disk Size (GB)";e={$_.Size /1GB}} -AutoSize
Get-WmiObject -Class win32_computersystem -ComputerName $computerName | ft @{Name="Physical Processors";e={$_.NumberofProcessors}} ,@{Name="Logical Processors";e={$_.NumberOfLogicalProcessors}} , @{Name="TotalPhysicalMemory (GB)";e={[math]::truncate($_.TotalPhysicalMemory /1GB)}}, Model -AutoSize
Get-WmiObject -Class win32_operatingsystem -ComputerName $computerName | ft @{Name="Total Visible Memory Size (GB)";e={[math]::truncate($_.TotalVisibleMemorySize /1MB)}}, @{Name="Free Physical Memory (GB)";e={[math]::truncate($_.FreePhysicalMemory /1MB)}} -AutoSize
Get-WmiObject -Class win32_operatingsystem -ComputerName $computerName | ft @{Name="Operating System";e={$_.Name}} -AutoSize
Get-WmiObject -Class win32_bios -ComputerName $computerName | ft @{Name="ServiceTag";e={$_.SerialNumber}}
}
Get-HostProperties

The TotalVisibleMemorySize Data type is: uint64. I mistakenly thought the property is in bytes, but it’s actually in kilobytes as indicated in an msdn documentation:“Total amount, in kilobytes, of physical memory available to the operating system. This value does not necessarily indicate the true amount of physical memory, but what is reported to the operating system as available to it” .

The GB and MB are PowerShell constants that convert the original bytes and kilobytes values to gigabytes. The Math::truncate() method simply truncates or chops off the decimal places.

A screen shot of the script result is shown below:

FreeMemory

Advertisements
This entry was posted in Active Directory Domain Services, AD Forest, Hyper-v, Hyper-v 2012 R2, PowerShell, PowerShell 3.0, Powershell 4.0, Script, Scripts, Windows Server 2012 R2 and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s