Type Accelerators are a great PowerShell feature that allows shortcuts to .NET classes. When writing .NET code, once a namespace is imported with the ‘using’ directive, types in that namespace can be used without referencing the namespace itself:

using System.Collections.Generic;
List l = new List();
// Instead of:
System.Collections.Generic.List l = new System.Collections.Generic.List();

However, it’s not possible to do that in PowerShell, which can make code somewhat lengthy. At RAVN, we use a number of library objects that have long namespace paths such as ‘Ravn.AutonomyTools.Idol.Query’. Scripts using these libraries can often become very verbose. Enter PowerShell Type Accelerators. You’ve probably been using them in your scripts without realising, here are some more common ones:

[hashtable] # System.Collections.Hashtable
[wmi]       # System.Management.ManagementObject
[psobject]  # System.Management.Automation.PSObject
[regex]     # System.Text.RegularExpressions.Regex

So how do we find more accelerators available in PowerShell? A dictionary of the available accelerators can be obtained by using the static ‘Get’ method of the ‘System.Management.Automation.TypeAccelerators’ class:

$accelerators = [System.Management.Automation.PSObject].Assembly.GetType(
    'System.Management.Automation.TypeAccelerators',
    $true,
    $true
)
$accelerators::Get

The TypeAccelerators class is private, thus the workaround through PsObject. As the ‘Get’ property returns a dictionary, it’s possible to return details of a particular accelerator, or to search by namespace:

$accelerators::Get['hashtable'].FullName

accelerators-get-specific

$Namespace = 'System.Collections'
$accelerators::Get.GetEnumerator() | ? { $_.Value.FullName -match "^$Namespace" }

accelerators-get-collections
So how can Type Accelerators be used to make code less verbose? It’s possible to add and remove custom type accelerators for any .NET class. System.Collections.List appears often in my scripts, and I’ve created the ‘strlist’ accelerator for it:

$Name = 'strlist'
$Type = 'System.Collections.Generic.List[string]'
$accelerators::Add($Name, $Type)

accelerators-new-strlist
It’s now possible to reference a string list using ‘strlist’:

[strlist]$l = @()
$l.Add("String")
$l.Add("List")
$l.GetType()
$l -join ','

accelerators-check-strlist
And what about removing accelerators that are no longer required? Just as simple as adding them:

$accelerators::Remove('strlist')

accelerators-remove-strlist
For our professional services library, I’ve added three functions: Add-TypeAccelerator, Get-TypeAccelerator, Remove-TypeAccelerator, as wrappers to the functionality detailed above. Now, on module import, custom accelerators are loaded, including ones like ‘autnquery’ to simplify the use of ‘Ravn.AutonomyTools.Idol.Query’.

Leave a Reply

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

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

clear formSubmit