A PowerShell Module for your Type Extensions

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.

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.

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.

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.

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.

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.

2 thoughts on “A PowerShell Module for your Type Extensions

  1. 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!

Leave a Reply

Your email address will not be published. Required fields are marked *