views:

344

answers:

4

In PowerShell you can use [xml] to mean [System.Xml.XmlDocument]. Do you know where I can find a list of these type accelerators?

Are these accelerators specific to PowerShell or .NET?

+4  A: 

See the section entitled Type Name Aliases in this blog post. I believe this is a complete list of the aliases.

PowerShell Type Alias   Corresponding .NET Type
[int]                   System.Int32
[int[]]                 System.Int32[]
[long]                  System.Int64
[long[]]                System.Int64[]
[string]                System.String
[string[]]              System.String[]
[char]                  System.Char
[char[]]                System.Char[]
[bool]                  System.Boolean
[bool[]]                System.Boolean[]
[byte]                  System.Byte
[byte[]]                System.Byte[]
[double]                System.Double
[double[]]              System.Double[]
[decimal]               System.Decimal
[decimal[]]             System.Decimal[]
[float]                 System.Single
[single]                System.Single
[regex]                 System.Text.RegularExpression.Regex
[array]                 System.Array
[xml]                   System.Xml.XmlDocument
[scriptblock]           System.Management.Automation.ScriptBlock
[switch]                System.Management.Automation.SwitchParameter
[hashtable]             System.Collections.Hashtable
[psobject]              System.Management.Automation.PSObject
[type]                  System.Type
[type[]]                System.Type[]
Noldorin
[wmi] appears to be one that is missing from the list
Tyson Gilberstad
@Tyson: Yeah, so I suspected one or two might be missing. Hopefully that will get you started though. Just search for specific ones if you find yourself wanting an alias. :)
Noldorin
There are a lot missing (including all of the 2.0 ones) and most of the ones in here aren't technically accelerators anyway. Use the method in Keith Hill's post to get a list (and optionally, to modify it). Any type that's in the "System" namespace might look like an accelerator, but it's just because you can *always* leave "System." off of the front of a type ... Array notations aren't separate accelerators either, because array notation is automatically available to any type, including type accelerators.
Jaykul
+2  A: 

@Noldorin has a good list of some of the Type Accelerators, with some.

PowerShell also allows you to use type literals to cast objects, call static methods, access static properties, reflect over, and anything else you might do with an instance of a System.Type object.

In order to use a type literal, you just enclose the full name (namespace and class name) of the class (or struct or enum) (with a period separating the namespace and the class name) enclosed in brackets like:

[System.Net.NetworkInformation.IPStatus]

PowerShell will also provide a leading "System." in its attempt to resolve the name, so you don't need to explicitly use that if you are using something in a System* namespace.

[Net.NetworkInformation.IPStatus]

Oisin Grehan (a PowerShell MVP) also has a blog post about creating your own type accelerators.

Steven Murawski
Interesting, thanks.
Tyson Gilberstad
See Oisin's newer post on the subject linked in Keith Hill's answer.
JasonMArcher
+5  A: 

The definitive way is to do what Oisin demontrates in this excellent blog post:

PS> $acceleratorsType = [type]::gettype("System.Management.Automation.TypeAccelerators")
PS> $acceleratorsType

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
False    False    TypeAccelerators                         System.Object


PS> $acceleratorsType::Add("accelerators", $acceleratorsType)
PS> [accelerators]::Get

Key                                                         Value
---                                                         -----
int                                                         System.Int32
...

Note that you have to add the new 'accelerators' accelerator to the dictionary because the TypeAccelerators type is not public. Amazing what you can do with .NET Reflector and a lot of spare time. :-) You rock Oisin!

This is a much better post to go by than Oisin's older post (which is kind of a hack).
JasonMArcher
I wrote a module to list, add, and remove custom type accelerators awhile ago ... I just posted an update to it on PoshCode: http://poshcode.org/1398
Jaykul
A: 

What does it mean when there are two brackets in front of the type name (ex. [byte[]])?

Roman
Why don't you ask a question?
Joey