Understanding Type Accelerators (Part 2)

by Jun 12, 2018

PowerShell comes with a number of hard-coded type accelerators that serve like aliases for commonly used .NET types, and since they are a lot shorter than the original type names, they “accelerate the typing”.

A little-known fact is that the list of type accelerators is extensible. The line below adds a new type accelerator named “SuperArray” that points to “System.Collections.ArrayList”

You can now create new “super arrays” (which behave just like regular arrays, but come with a bunch of extra methods to insert or remove elements at arbitrary positions, and append array elements much faster than with regular arrays):

[PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Add('SuperArray', [System.Collections.ArrayList])

$a = [superarray]::new()

You can also convert regular arrays into “super arrays”:

 
PS> $a = [superarray](1,2,3)

PS> $a.RemoveAt(1)

PS> $a 
1
3   
 

Note however that you could have done the same without type accelerators. They just save typing:

 
PS> $a = [System.Collections.ArrayList](1,2,3)

PS> $a.RemoveAt(1)

PS> $a 
1
3  
 

Twitter This Tip! ReTweet this Tip!