Add a type Accelerator to PowerShell


Ever wanted to See what type accelerators you have in your current session or better yet add one to your current session.

Well this article will show you two functions for this type of thing:

Function Get-TypeAccelerators{
[psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::get
}
Function Add-TypeAcclerator {

param([string]$acceleratorName, [string]$acceleratorClassName)

$accel =[PowerShell].assembly.gettype("System.Management.Automation.TypeAccelerators")
$accleratorAdd = "`$accel::Add(`"$acceleratorName`",[$acceleratorClassName])"
Invoke-expression $accleratorAdd
$builtinField = $accel.Getfield("builtinTypeAccelerators", [System.Reflection.BindingFlags]"Static,NonPublic")
$builtinField.SetValue($builtinField, $accel::Get)
}
Add-TypeAcclerator -acceleratorName FileInfo -acceleratorClassName System.IO.FileSystemInfo

Now if you type in get-typeaccelerators you’ll see the one you added:


Get-TypeAccelerators

Key                          Value                                                              
---                          -----                                                              
Alias                        System.Management.Automation.AliasAttribute                        
AllowEmptyCollection         System.Management.Automation.AllowEmptyCollectionAttribute         
AllowEmptyString             System.Management.Automation.AllowEmptyStringAttribute             
....                   
FileInfo                     System.IO.FileSystemInfo                                           

See this article for further information:

Adding Type Accelerators in the PowerShell 5.0 April 2015 Preview – TechNet Articles – United States (English) – TechNet Wiki (microsoft.com)

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s