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")

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

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

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


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

PS> [string].FullName

PS> [int].FullName

PS> [wmi].FullName

PS> [adsi].FullName


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

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


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')

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!