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:
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\/\/