Extending Win32_PhysicalMemory with Update-TypeData

CMDLET Name : Update-TypeData

What it does:

Let’s you add new properties, modify existing properties, add new methods, define a key property to sort databy, define exactly what properties you want to see when the cmdlet is typed without any select statements.

Every object in PowerShell has a Type associated with it that defines the object.

For example :
            $str is a string and so it has a typename of : System.String
            $number is a integer so has a typename of : System.Int32
            $date is a date so has a typename of : System.DateTime

PS C:\> $str = “PowerShell”

 

PS C:\> $str | Get-Member

 

   TypeName: System.String

                                                                                                                                                                                       

 

PS C:\> $number = 3

 

PS C:\> $number | Get-Member

 

   TypeName: System.Int32

        

PS C:\> $date = get-date

 

PS C:\> $date | Get-Member

 

   TypeName: System.DateTime

 

 

and so on and so forth.

By default WMI produces a lot of output which makes it difficult to get to that one property or properties that you are looking for.Type the command below and see for yourself:

The way out of this is to use a Select statement to view a few select properties like so:

 

If the above command is something that you would be typing in frequently wouldn’t it be nice to get back the same set of properties every time without having to include a Select statement. Well you are in luck because PowerShell V3 has introduced a cmdlet – Update-TypeData, to do exactly that.

Update-Typedata accepts a typename as a parameter so lets get the typename for Win32_physicalmemory.

PS C:\> Get-WmiObject -Class win32_physicalmemory | Get-Member

 

   TypeName: System.Management.ManagementObject#root\cimv2\Win32_PhysicalMemory

 

 

Store the typename in a variable $PhysicalMemory_Type.

PS C:\> $PhysicalMemory_Type =”System.Management.ManagementObject#ROOT\cimv2\Win32_PhysicalMemory”

 

PS C:\> $PhysicalMemory_Type

System.Management.ManagementObject#ROOT\cimv2\Win32_PhysicalMemory

 

Define a custom view –> DefaultdisplayPropertySet using the properties below:

PSComputerName, BankLabel, DataWidth, MemoryType, Capacity

PS C:\> Update-TypeData -TypeName $PhysicalMemory_Type -DefaultDisplayPropertySet PSComputerName,BankLabel,DataWidth,MemoryType,Capacity -Force

 

Note :   We need to use the –Force parameter because we will be overwriting the one that has already been defined.

Re-running the command:

PS C:\> Get-WmiObject -Class win32_physicalmemory

 

 

PSComputerName : VM1

BankLabel      : RAM slot #0

DataWidth      : 32

MemoryType     : 2

Capacity       : 2147483648

 

PSComputerName : VM1

BankLabel      : RAM slot #1

DataWidth      : 32

MemoryType     : 2

Capacity       : 1073741824

 

PSComputerName : VM1

BankLabel      : RAM slot #2

DataWidth      : 32

MemoryType     : 2

Capacity       : 536870912

 

PSComputerName : VM1

BankLabel      : RAM slot #3

DataWidth      : 32

MemoryType     : 2

Capacity       : 268435456

 

PSComputerName : VM1

BankLabel      : RAM slot #4

DataWidth      : 32

MemoryType     : 2

Capacity       : 134217728

 

PSComputerName : VM1

BankLabel      : RAM slot #5

DataWidth      : 32

MemoryType     : 2

Capacity       : 33554432

 

PSComputerName : VM1

BankLabel      : RAM slot #6

DataWidth      : 32

MemoryType     : 2

Capacity       : 16777216

 

Now we are only seeing the properties that we defined in the DefaultdisplayPropertySet . We can easily add or remove properties from the DefaultdisplayPropertySet by re-running the update-typedata cmdlet. For example if we only wanted the properties : Pscomputername, banklabel & Capacity

we would do this:

PS C:\> Update-TypeData -TypeName $PhysicalMemory_Type -DefaultDisplayPropertySet PSComputerName,BankLabel,Capacity -Force

 

 

PS C:\> Get-WmiObject -Class win32_physicalmemory

 

PSComputerName BankLabel     Capacity

————– ———     ——–

VM1         RAM slot #0 2147483648

VM1         RAM slot #1 1073741824

VM1         RAM slot #2  536870912

VM1         RAM slot #3  268435456

VM1         RAM slot #4  134217728

VM1         RAM slot #5   33554432

VM1         RAM slot #6   16777216

 

 

if you changed your mind and wanted to go back to the defaults, kind of like doing a reset, simply do this:

lets go back to our old view, but this time instead of Capacity we will be adding a new property CapacityMB .

 

Note :  The CapacityMB property wont be found on the Win32_physicalmemory class.This is a dynamic property that we add in order to display the capacity values in megabytes.

 

We have added our new property into the view but we haven’t provided it with a value yet. so lets do that now.

PS C:\> Update-TypeData -TypeName $PhysicalMemory_Type -MemberType ScriptProperty  -MemberName CapacityMB -Value {$this.capacity/1MB}

 

Note :   $This in the command above represents the current value being processed while iterating through a collection. To convert bytes into Megabytes we divide capacity by 1MB but if we wanted to display the values in Gigabyte we would divide capacity by 1GB(No space between 1 & GB).

 

Lets re-run the command:

PS C:\> Get-WmiObject -Class win32_physicalmemory

 

 

PSComputerName : HOBBES

BankLabel      : RAM slot #0

DataWidth      : 32

MemoryType     : 2

CapacityMB     : 2048

 

PSComputerName : HOBBES

BankLabel      : RAM slot #1

DataWidth      : 32

MemoryType     : 2

CapacityMB     : 1024

 

PSComputerName : HOBBES

BankLabel      : RAM slot #2

DataWidth      : 32

MemoryType     : 2

CapacityMB     : 512

 

PSComputerName : HOBBES

BankLabel      : RAM slot #3

DataWidth      : 32

MemoryType     : 2

CapacityMB     : 256

 

PSComputerName : HOBBES

BankLabel      : RAM slot #4

DataWidth      : 32

MemoryType     : 2

CapacityMB     : 128

 

PSComputerName : HOBBES

BankLabel      : RAM slot #5

DataWidth      : 32

MemoryType     : 2

CapacityMB     : 32

 

PSComputerName : HOBBES

BankLabel      : RAM slot #6

DataWidth      : 32

MemoryType     : 2

CapacityMB     : 16

 

Good, so we now have capacity values in Megabytes. The built-in property capacity is still available so lets do a comparison:

PS C:\> Get-WmiObject -Class win32_physicalmemory | select Capacity, CapacityMB

 

  Capacity CapacityMB

  ——– ———-

2147483648       2048

1073741824       1024

 536870912        512

 268435456        256

 134217728        128

  33554432         32

  16777216         16

 

it’s obvious to see which one is easier to read.

The MemoryType property value of 2 means DRAM. While we can look up the MSDN documentation and determine what that means we would have to do this every time the value changes. so for example if the MemoryType value returned were 3 what would that translate to? we again look up the documentation. As you can imagine this can get a bit tedious, so lets do our translation once and be done with it.

There are a total of 21 different Memory types, I am  just showing the first 7 –  MSDN Link

image

We will modify our view by replacing MemoryType with MemoryTypeString.

 

Lets add some code that will translate the MemoryType integer values into string values and store them in the variable MemoryTypeString

 

Since this is a pretty long piece of code I would prefer to do this in the PowerShell_ISE

Lets re-run the command:

PS C:\> Get-WmiObject -Class win32_physicalmemory

 

 

PSComputerName   : HOBBES

BankLabel        : RAM slot #0

DataWidth        : 32

MemoryTypeString : DRAM

CapacityMB       : 2048

 

PSComputerName   : HOBBES

BankLabel        : RAM slot #1

DataWidth        : 32

MemoryTypeString : DRAM

CapacityMB       : 1024

 

PSComputerName   : HOBBES

BankLabel        : RAM slot #2

DataWidth        : 32

MemoryTypeString : DRAM

CapacityMB       : 512

 

PSComputerName   : HOBBES

BankLabel        : RAM slot #3

DataWidth        : 32

MemoryTypeString : DRAM

CapacityMB       : 256

 

PSComputerName   : HOBBES

BankLabel        : RAM slot #4

DataWidth        : 32

MemoryTypeString : DRAM

CapacityMB       : 128

 

PSComputerName   : HOBBES

BankLabel        : RAM slot #5

DataWidth        : 32

MemoryTypeString : DRAM

CapacityMB       : 32

 

PSComputerName   : HOBBES

BankLabel        : RAM slot #6

DataWidth        : 32

MemoryTypeString : DRAM

CapacityMB       : 16

 

Looking better.

If we wanted to view the output in a table format instead of a list we can do this:

PS C:\> Get-WmiObject -Class win32_physicalmemory | Format-Table -AutoSize

 

PSComputerName BankLabel   DataWidth MemoryTypeString CapacityMB

————– ———   ——— —————- ———-

HOBBES         RAM slot #0        32 DRAM                   2048

HOBBES         RAM slot #1        32 DRAM                   1024

HOBBES         RAM slot #2        32 DRAM                    512

HOBBES         RAM slot #3        32 DRAM                    256

HOBBES         RAM slot #4        32 DRAM                    128

HOBBES         RAM slot #5        32 DRAM                     32

HOBBES         RAM slot #6        32 DRAM                     16

 

But lets be lazy Smile and get the command to output a table without typing in Format-Table –AutoSize. Since this will involve working with another Cmdlet – Update-FormatData I will be covering it in the next article.

Points to Remember:

The DefaultdisplayPropertySet along with any custom properties that you add is not persistent in other words if you close and re-open the console the custom type information will be erased and all the views and values will go back to their defaults. So in order to make your custom settings persistent you can either put your commands in your profile or put them in a separate script and dot source that script in your profile.

image

Learn more about the PowerShell Profile here & here

If you want to see the complete set of properties for the type you would run:

To view the currently defined DefaultdisplayPropertySet you would run:

 

To get more information on a Type, run the command below:

Obviously you would replace the variable $PhysicalMemory_Type with whatever type you are working with.

Leave a Comment Yourself

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">