Saturday, December 06, 2014

Type Accelerators – And a Module

Over the years, I've written about type accelerators and PowerShell. A type accelerator is simply a synthetic type name that you can create to serve as an alias to another type name. For example, you could use the int16 type to refer to the System.Int16, or guid instead of [system.guid]. Type accelerators are meant to simplify handling of types by IT Pros in PowerShell scripts.

TAs were first introduced in a beta of PowerShell V2, and I posted code that worked (then). However, I have been recently updating my script library blog (http://pshscripts.blogspot.com), I discovered that TAs stopped working that way. After some fun and effort, I've now found hot how they work today, and how you can play with them!

TAs work by just providing you with an alias to a full type name. To some degree, TAs make up for the lack of a Using statement such as in the C# language. Some common TAs in use include [ADSI], [WMI], [String], [regex] and many more.

This week, I have published a new TypeAccelerators modules. This module contains scripts that define 4 TA-related functions:

  • Get-TypeAccelerator– this gets the TAs active in your current PowerShell Session. This function also takes a parameter (AliasRE) which is a regular expression over the type accelerators to return.
  • Measure-TypeAccelerator – this just returns a count of the TAs active.
  • New-TypeAccelerator – this adds a new type accelerator that aliases an existing type
  • Remove-TypeAccelerator – this removes a type accelerator from your current system.

Here's a screen shot of using these TAs

 image

The scripts also create aliases for these functions: GTA, CTA, NTA and RTA as you might imagine. The module also includes an about_ file for help – you get this importing the module!

You can get the module at http://www.reskit.net/powershell/typeacceleratosr.zip. I will also look to publish this module to the new PowerShell V5 Internet module repository (and maybe Chocolatey too if I get time).

A small footnote: this module and the scripts therein are intended to illustrate the concepts around type accelerators. They lack any sort of pipeline friendliness or error handling. So if you decide to make use of the concepts here – make sure you add the production quality aspects to your code, leveraging mine.

1 comment:

Unknown said...

Hi Thomas,

You know this is already covered by TypePx in the PowerShell Gallery, right? Instead of duplicating effort and contributing to confusion in the community (especially since you duplicate aliases as well, which may break scripts if our two modules are not compatible with one another) if you have additional value to add above what is already available why not contribute to a shared module by sending a pull request to my module at https://github.com/KirkMunro/TypePx? That would be a better approach than this module.

Kirk out.