Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

PowerShell V2 Add-AcceleratorType function

As I explained TypeAccelerators on IRC, Jaykul mentioned how cool it would be able to make your own, 

I came up with a very simple Add-AcceleratorType function that makes this at least a bit easier ,

it works by creating a type with a New Method and you can give a name, returntype and the code to implement the new method, that function gives the ability to add a shortcut Type in PowerShell that allows you to call the new() method to create the object:

image

I made the Add-AcceleratorType function to do this , you need to add extra assemblies you need to the C# code :

Function Add-AcceleratorType ($name,[string]$type,$code) {

$code = @"
  public class $name
  {
      public static $type New() {$code}
  }
"@

Add-Type $code -ReferencedAssemblies System.DirectoryServices

}

# examples

Add-AcceleratorType rnd System.Random 'return new System.Random();'
Add-AcceleratorType Domain System.DirectoryServices.ActiveDirectory.Domain 'return System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain();'

Not as handy as a real type accelerator, but it is very helpful when working with long type names in PowerShell see the AD example

[System.DirectoryServices.ActiveDirectory.Domain]::.GetCurrentDomain()

or

[domain]::new()

even with PowerTab this shortcut helps a lot ;-)

Enjoy,

Greetings /\/\o\/\/

Published Friday, September 12, 2008 5:03 PM by MoW
Filed under: ,

Comments

# re: PowerShell V2 Add-AcceleratorType function

I'm not sure that this is a good thing but I love the fact that you can do it and delighted at the thinking behind it - that is super clever.  

jps

Saturday, September 13, 2008 10:36 AM by jsnover

# PowerShell Accelerators - Richard Siddaway's Blog

Wednesday, September 17, 2008 4:06 AM by PowerShell Accelerators - Richard Siddaway's Blog
Anonymous comments are disabled