#PSTip Extending Type Data

Note: This tip requires PowerShell 3.0 or above.

In the previous article Matt used lambda functions to simplify data manipulation . In this tip I want to show you a way of using functions as methods by extending the System.Array class.

Beginning in Windows PowerShell 3.0 we can use the Update-TypeData to update types with new type data. In previous versions of PowerShell this operation required writing a ps1xml file (lots of XML to deal with). Here’s an example that adds a _‘map’ script _method to Array objects :

Update-TypeData -Force -MemberType ScriptMethod -MemberName map -TypeName System.Array -Value {
    if($o -is [int]) { $this | foreach {$_ * $o} }



One caveat of the new added method is its signature. When viewed using the Get-Member cmdlet we can see that the method doesn’t accept any arguments though the script block used to extend the type is using a param block with one parameter ($object).

PS> Get-Member -InputObject @(1..5) -MemberType ScriptMethod

   TypeName: System.Object[]
Name MemberType   Definition
---- ----------   ----------
map  ScriptMethod System.Object map();

In the same manner , we can use Update-TypeData to “attach” new functionality to .NET types. Check the help of Update-TypeData for more information and code examples.

Share on: