Simplifying .NET Types

by Jan 15, 2015

All PowerShell versions

PowerShell uses short names for the most common .NET types. To see if there is a short name for a .NET type you are using, try this:

 
PS> [System.Management.Automation.LanguagePrimitives]::ConvertTypeNameToPSTypeName("System.String")
[string]

PS> [System.Management.Automation.LanguagePrimitives]::ConvertTypeNameToPSTypeName("System.Int32")
[int]

PS> [System.Management.Automation.LanguagePrimitives]::ConvertTypeNameToPSTypeName("System.Management.ManagementObject")
[wmi]

PS> [System.Management.Automation.LanguagePrimitives]::ConvertTypeNameToPSTypeName("System.DirectoryServices.DirectoryEntry")
[adsi]

PS>  
 

To find the real .NET name and go the other way, try this:

 
PS> [string].FullName
System.String

PS> [int].FullName
System.Int32

PS> [wmi].FullName
System.Management.ManagementObject

PS> [adsi].FullName
System.DirectoryServices.DirectoryEntry

PS>  
 

With these tricks, you can also better understand how PowerShell converts certain data types:

 
PS> [System.Management.Automation.LanguagePrimitives]::ConvertTypeNameToPSTypeName("UInt8")
[Byte]

PS>
 

As it turns out, whenever PowerShell hits an unsigned 8-bit integer value, it auto-converts it to a Byte. This entire magic is handled by ConvertTypeNameToPSTypeName(). Internally, PowerShell uses a lookup table to convert certain data types:

$field = [System.Management.Automation.LanguagePrimitives].GetField('nameMap', 'NonPublic,Static')
$field.GetValue([System.Management.Automation.LanguagePrimitives]) 

The map looks like this:

 
Key                                                            Value                                                        
---                                                            -----                                                        
SInt8                                                          SByte                                                        
UInt8                                                          Byte                                                         
SInt16                                                         Int16                                                        
UInt16                                                         UInt16                                                       
SInt32                                                         Int32                                                        
UInt32                                                         UInt32                                                       
SInt64                                                         Int64                                                        
UInt64                                                         UInt64                                                       
Real32                                                         Single                                                       
Real64                                                         double                                                       
Boolean                                                        bool                                                         
String                                                         string                                                       
DateTime                                                       DateTime                                                     
Reference                                                      CimInstance                                                  
Char16                                                         char                                                         
Instance                                                       CimInstance                                                  
BooleanArray                                                   bool[]                                                       
UInt8Array                                                     byte[]                                                       
SInt8Array                                                     Sbyte[]                                                      
UInt16Array                                                    uint16[]                                                     
SInt16Array                                                    int64[]                                                      
UInt32Array                                                    UInt32[]                                                     
SInt32Array                                                    Int32[]                                                      
UInt64Array                                                    UInt64[]                                                     
SInt64Array                                                    Int64[]                                                      
Real32Array                                                    Single[]                                                     
Real64Array                                                    double[]                                                     
Char16Array                                                    char[]                                                       
DateTimeArray                                                  DateTime[]                                                   
StringArray                                                    string[]                                                     
ReferenceArray                                                 CimInstance[]                                                
InstanceArray                                                  CimInstance[]                                                
Unknown                                                        UnknownType
 

Twitter This Tip! ReTweet this Tip!