Tyson Gilberstad
Tyson Gilberstad

Reputation: 223

Where can I find a list of Powershell .NET Type Accelerators?

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?

Upvotes: 18

Views: 6560

Answers (5)

Keith Hill
Keith Hill

Reputation:

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!

Upvotes: 12

Saintali
Saintali

Reputation: 4571

Here is a more complete list:

Key                   Value
---                   -----
adsi                  System.DirectoryServices.DirectoryEntry
adsisearcher          System.DirectoryServices.DirectorySearcher
array                 System.Array
bigint                System.Numerics.BigInteger
bool                  System.Boolean
byte                  System.Byte
char                  System.Char
cimclass              Microsoft.Management.Infrastructure.CimClass
cimconverter          Microsoft.Management.Infrastructure.CimConverter
ciminstance           Microsoft.Management.Infrastructure.CimInstance
cimtype               Microsoft.Management.Infrastructure.CimType
cultureinfo           System.Globalization.CultureInfo
datetime              System.DateTime
decimal               System.Decimal
double                System.Double
float                 System.Single
guid                  System.Guid
hashtable             System.Collections.Hashtable
initialsessionstate   System.Management.Automation.Runspaces.InitialSessionState
int                   System.Int32
int16                 System.Int16
int32                 System.Int32
int64                 System.Int64
ipaddress             System.Net.IPAddress
long                  System.Int64
mailaddress           System.Net.Mail.MailAddress
powershell            System.Management.Automation.PowerShell
psaliasproperty       System.Management.Automation.PSAliasProperty
pscredential          System.Management.Automation.PSCredential
pscustomobject        System.Management.Automation.PSObject
pslistmodifier        System.Management.Automation.PSListModifier
psmoduleinfo          System.Management.Automation.PSModuleInfo
psnoteproperty        System.Management.Automation.PSNoteProperty
psobject              System.Management.Automation.PSObject
psprimitivedictionary System.Management.Automation.PSPrimitiveDictionary
psscriptmethod        System.Management.Automation.PSScriptMethod
psscriptproperty      System.Management.Automation.PSScriptProperty
psvariable            System.Management.Automation.PSVariable
psvariableproperty    System.Management.Automation.PSVariableProperty
ref                   System.Management.Automation.PSReference
regex                 System.Text.RegularExpressions.Regex
runspace              System.Management.Automation.Runspaces.Runspace
runspacefactory       System.Management.Automation.Runspaces.RunspaceFactory
sbyte                 System.SByte
scriptblock           System.Management.Automation.ScriptBlock
securestring          System.Security.SecureString
single                System.Single
string                System.String
switch                System.Management.Automation.SwitchParameter
timespan              System.TimeSpan
type                  System.Type
uint16                System.UInt16
uint32                System.UInt32
uint64                System.UInt64
uri                   System.Uri
version               System.Version
void                  System.Void
wmi                   System.Management.ManagementObject
wmiclass              System.Management.ManagementClass
wmisearcher           System.Management.ManagementObjectSearcher
xml                   System.Xml.XmlDocument

Upvotes: 1

Michael Sorens
Michael Sorens

Reputation: 36768

Since this question was asked and answered four years ago PowerShell has continued to evolve. @KeithHill's concise answer unfortunately no longer works. I did a little digging and found that the requisite class is just a bit less exposed. On the bright side, the list of type accelerators may now be displayed with just this one line of code...

[psobject].assembly.gettype("System.Management.Automation.TypeAccelerators")::Get

... attributed to Jaykul in this Connect post.

Here is a partial output:

Key                    Value
---                    -----
Alias                  System.Management.Automation.AliasAttribute
AllowEmptyCollection   System.Management.Automation.AllowEmptyCollectionAttribute
AllowEmptyString       System.Management.Automation.AllowEmptyStringAttribute
AllowNull              System.Management.Automation.AllowNullAttribute
array                  System.Array
bool                   System.Boolean
byte                   System.Byte
char                   System.Char
CmdletBinding          System.Management.Automation.CmdletBindingAttribute
datetime               System.DateTime
decimal                System.Decimal
adsi                   System.DirectoryServices.DirectoryEntry
adsisearcher           System.DirectoryServices.DirectorySearcher
double                 System.Double
float                  System.Single
single                 System.Single
guid                   System.Guid
hashtable              System.Collections.Hashtable
int                    System.Int32
. . .

2014.03.15 Update

As of PowerShell Community Extensions (PSCX) version 3.1.0, you can now use a type accelerator to list all type accelerators and just invoke this:

[accelerators]::get

Upvotes: 13

Steven Murawski
Steven Murawski

Reputation: 11255

@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.

Upvotes: 3

Noldorin
Noldorin

Reputation: 147461

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[]

Upvotes: 8

Related Questions