Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

A PowerShell Module for your Type Extensions

Posted on November 13, 2017

If you've been following this blog recently, you've read about my fun with PowerShell type extensions. This technique lets you make PowerShell give you the information you want without a lot of work on your part. Well, there is some work but you only have to do it once. To make it even easier, I have been working on a module to simplify this even further. The module is still in beta so I'm hoping some of you will kick it around before I publish it to the PowerShell Gallery.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

The module, PSTypeExtensionTools, is available on Github at https://github.com/jdhitsolutions/PSTypeExtensionTools. Download the latest beta release zip file and extract it to a working directory.  Unless you put it in one of the pre-defined module directories, import the module: Import-Module c:\work\pstypeextensiontools.

image

The command to create new type extensions is Add-PSTypeExtension. You specify the type name, the member type, a name and a value. Underneath the hood this command is running Update-TypeData but I wanted to simplify the process a bit and take advantage of some pipeling processes. For example, you may not know off the top of your head the type name of the object you want to enhance, or it is a long type name and you prefer not to type it. Get-PSType will return the type name.

image

I did this so I (meaning you) could run a command like this to add a new type extension.

123 | Get-PSType | Add-PSTypeExtension -MemberType ScriptProperty -MemberName SquareRoot -Value { [math]::Sqrt($this)}

You can create any property type except CodeProperty and CodeMethod. These are bit more complicated and move, in my opinion, outside the realm of scripting. If you need to define these types of members, you don't need this module. Of course, once you know the type name you can create as many extensions as you'd like.

Add-PSTypeExtension -TypeName system.int32 -MemberType ScriptProperty -MemberName Squared -value { $this*$this}
Add-PSTypeExtension -TypeName system.int32 -MemberType ScriptProperty -MemberName Cubed -value { [math]::Pow($this,3)}
Add-PSTypeExtension -TypeName system.int32 -MemberType ScriptProperty -MemberName Value -value { $this}
Add-PSTypeExtension -TypeName system.int32 -MemberType ScriptMethod -MemberName GetPercent -value {Param([int32]$Total,[int32]$Round=2) [math]::Round(($this/$total)*100,$round)}

image

Want to know what has been defined for a given type? Use Get-PSTypeExtension. The default is to prompt you for a member name, or use the –All parameter.

image

I'm thinking I need to modify the behavior so that showing all members is the default behavior. Now for the fun part. In order for these extensions to persist you would need to re-define them in your PowerShell profile. But perhaps you don't remember how you defined them. Or you want an easier way. You can use Export-PSTypeExtension and export the desired members to either a json or xml file.

Get-PSTypeExtension system.int32 -all | Export-PSTypeExtension -TypeName system.int32 -Path c:\work\int32-types.json

Do this for as many different types as you'd like.

image

You should check types first and only export members you have defined.

SNAGHTML28d84173

There's no reason to export anything else that is defined automatically. The export command will use with ConvertTo-Json or Export-Clixml depending on the file extension. In your profile all you need to do is import the type extensions.

Import-PSTypeExtension -Path C:\work\int32-types.json

Easy and it keeps my profile script simple. I've created a Samples folder in the GitHub repo if you want to download and try. The samples are not part of the beta zip file but that will probably change with the next release.

I hope a few of you will give this a try and let me know what you think. Use the Issues section to report any problems, comments or suggestions.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

3 thoughts on “A PowerShell Module for your Type Extensions”

  1. Vince says:
    November 21, 2017 at 2:27 pm

    I really like this. The samples are great, too — I had a need to randomize a string just this week.

    A couple of small things:
    1. The link in the “I’ve created a Samples folder in the GitHub repo…” gets a 404.
    2. In the samples (I got there from the main GitHub page), the cimlogicaldisk-extensions.json file contains what appears to be three identical copies of the three extensions. I’ve gone so far as to save two of the copies to disk and diff’d them to make sure I wasn’t missing something (I assumed I was). Is that a glitch in the export, or cut-and-paste error, or something else?

    Hah, I just noticed the samples link appears to have been fixed while I was typing this, so thanks for that!

    1. Jeffery Hicks says:
      November 21, 2017 at 2:55 pm

      Looks like I botched that Samples link. I’ll double check the json files as well.

  2. Pingback: New PowerShell Projects Published

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d