Type accelerators

A type accelerator is an alias for a type name. At the beginning of this chapter, the System.Management.Automation.PowerShell type was used. This type has an accelerator available. The accelerator allows the following notation to be used:

[PowerShell].Assembly 

Another commonly used example is the ADSI accelerator. This represents the System.DirectoryServices.DirectoryEntry type. This means that the following two commands are equivalent:

[System.DirectoryServices.DirectoryEntry]"WinNT://$env:COMPUTERNAME" 
[ADSI]"WinNT://$env:COMPUTERNAME" 

Getting the list of type accelerators isn't quite as easy as it should be. An instance of the TypeAccelerators type is required first. Once that has been retrieved, a static property called Get will retrieve the list; the first few results are shown as follows:

$type = [PowerShell].Assembly.GetType('System.Management.Automation.TypeAccelerators') 
$type::Get 

New accelerators may be added; for example, an accelerator for the TypeAccelerators class would make life easier. To do this, an accelerator with the name Accelerators is added, using the TypeAccelerators type as the object that it references:

$type = [PowerShell].Assembly.GetType('System.Management.Automation.TypeAccelerators') 
$type::Add('Accelerators', $type) 

Once the new accelerator has been added, the previous operations can be simplified. Getting the list of accelerators is now done as follows:

[Accelerators]::Get 

New accelerators may be added using the following syntax:

[Accelerators]::Add('<Name>', [<TypeName>]) 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.22.169