Get-PhysicalMemory.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<#
.SYNOPSIS
Get the total, used, and free physical memory of a local or remote computer.
 
.DESCRIPTION
Get the total, used, and free physical memory of a local or remote computer.
 
.NOTES
Created by: Jason Wasser
Modified: 1/30/2015
Version 1.0
 
.EXAMPLE
Get-PhysicalMemory
Get the physical memory of the localhost.
 
.EXAMPLE
Get-PhysicalMemory -ComputerName SERVER3
Get the physical memory of SERVER3
 
.EXAMPLE
Get-PhysicalMemory -BaseUnit MB
Get the physical memory in Megabytes.
#>

function Get-PhysicalMemory
{
    [CmdletBinding()]
    [Alias('Get-RAM')]
    Param
    (
        # ComputerName
        [Alias("name")]
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [string[]]$ComputerName=$env:COMPUTERNAME,
        [Parameter(Mandatory=$false)]
        [ValidateSet("MB","GB")]
        [string]$BaseUnit="GB"

    )

    Begin
    {
    }
    Process
    {
        foreach ($Computer in $ComputerName) {
            if ($BaseUnit -eq "MB") {
                Get-WmiObject -Class win32_operatingsystem -ComputerName $Computer | Format-Table -Property PSComputerName,@{name="TotalPhysicalMemory(MB)";e={$_.TotalVisibleMemorySize}},@{name='UsedPhysicalMemory(MB)';e={($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)}},@{name='FreePhysicalMemory(MB)';e={$_.FreePhysicalMemory}} -AutoSize
                }
            elseif ($BaseUnit -eq "GB") {
                Get-WmiObject -Class win32_operatingsystem -ComputerName $Computer | Format-Table -Property @{name='ComputerName';e={$_.PSComputerName}},@{name="TotalPhysicalMemory(GB)";e={"{0:N2}" -f ($_.TotalVisibleMemorySize / 1MB)}},@{name='UsedPhysicalMemory(GB)';e={"{0:N2}" -f (($_.TotalVisibleMemorySize - $_.FreePhysicalMemory) / 1MB) }},@{name='FreePhysicalMemory(GB)';e={"{0:N2}" -f ($_.FreePhysicalMemory / 1MB)}} -AutoSize
                }
            else {
                Write-Error "Invalid BaseUnit $BaseUnit."
                }
            }
    }
    End
    {
    }
}