PowerShell Data Type Basics

Yellow Binary Byte 42

A year ago? A year and a half ago? Yes, that is how old some of the notes are that I left myself, after students asked for a list of the basic data types available in Windows PowerShell. I honestly thought I had blogged about this long ago. This article provides a foundation for several others. The question which has prompted this is one of several which people tend to ask during PowerShell courses.

What data types are available in Windows PowerShell?

Is there a list of these?

The quick answers to these are yes there is a list, and then I typically direct students to the Microsoft Developer Network (MSDN) .NET type reference pages. Here, we shall look at more specific answer which hopefully ties together several aspects of PowerShell and .NET data types that might not be clear from reading any one particular page on MSDN, TechNet, or various other PowerShell blogs or references. I hope that this helps. Please let me know if you would like any additional information.

If you are interested in PowerShell but either have no background or no interest in programming in C#, F#, J#, Visual Basic (VB), or any other the other lovely .NET programming languages, you may find that much of the .NET type information is not exactly Greek, but tends to be C# oriented, which many people have told me is disconcerting when they really just want to focus on Windows PowerShell.

First, it is good to note, before we get mired in details, that PowerShell typically does not require you to specify data types, but will automagically coerce values into the necessary underlying data types. Later, we’ll look at PowerShell as a Calculator, which strives to drive home this point. In summary, you usually don’t need to worry about data types in PowerShell, as they are handled transparently so that us humans do not have to be concerned with those gory details.

If you are concerned with the gory details for whatever reason, read on!

In Windows PowerShell, data types can be explicitly specified in several scenarios:

  • After the New-Object cmdlet name, a data type name can be given to create an instance (object) of that type.
  • Inside square brackets, a type name could be used to access a static (class) method or property.
  • Inside square brackets, a type name could be used to cast (convert) a value into that data type.

One of the first data types that people like to introduce is the integer data type, which in .NET is called Int32 starting with a capital I, in C# this is simply int with a lowercase i, and in the class method and casting uses in PowerShell is most often written in square brackets as lowercase int similar to C#, as follows: [int].

Note that PowerShell is flexible, and instead of using the C# notation int in square brackets, [int], you could refer to the .NET name within the System namespace by using the notation: [System.Int32], or even without the System namespace explicitly identified, as just [Int32]. Notice the capital I again in Int32. Also, because PowerShell is particularly forgiving with respect to case sensitivity, being largely case insensitive, unlike C#, we would also use [int32] or [system.int32] as well, or even [INT], [INT32], or [SYSTEM.INT32].

Real numbers which have a fractional part after the decimal point are a different data type, as shown by the result of the following logical expression:

PS C:> 42 -eq 42.1234
False

The -eq operator checks for equality of the operands, 42 on the left, and 42.1234 on the right. They are not equal, thus the result of the -eq operator is the boolean value False.

If we include a cast of the right-hand side to an integer value prior to the comparison, we have equality. Let’s look at this using two similar ways of specifying an integer cast in PowerShell:

PS C:> 42 -eq [int]42.1234
True
PS C:> 42 -eq [System.Int32]42.1234
True

Rather than dive deeper into casting, operators, expressions, and scenarios of when and how we could use explicit data types in Windows PowerShell, let’s get back to the original questions.

What are the data types available in Windows PowerShell? There are thousands by default, and you can easily add dozens, hundreds, or thousands more by bringing in additional software to be accessed and/or managed via PowerShell. Here is a brief and by no means exhaustive list of a small very few of the primitive types in PowerShell with a few commonly used non-primitive ones thrown in at the end of the list. Note that the use of the System namespace and the dot which separates that from the class/type name for the first two was used intentionally.

[System.Object]
[System.ValueType]
[void]
[bool]
[enum]
[byte]
[sbyte]
[int16]
[uint16]
[char]
[int]
[int32]
[uint32]
[float]
[single]
[long]
[int64]
[uint64]
[double]
[DateTime]
[decimal]
[guid]
[array]
[string]
[hashtable]
[random]
[regex]
[XML]
[WMI]
[WMICLASS]
[ADSI]

Consider the following PowerShell command snapshot, which is transcribed beneath the graphic and rendered as text for clarity.

2013w10-PowerShell-Types

Here is the same thing in a larger typeface (font) as text, just the one-line command first.

PS C:> [object],[valuetype],[void],[bool],[byte],[int],[long],[single],[double],[datetime],[decimal],[guid],[array],[string],[hashtable],[random],[regex],[PSObject],[XML],[WMI],[WMICLASS],[ADSI],[Diagnostics.Process],[ServiceProcess.ServiceController],[IO.FileInfo],[IO.DirectoryInfo] | FT Name,IsClass,IsValueType,UnderlyingSystemType,BaseType -auto

Note that in this example, we did not include the System namespace before each of the data types. Now, let’s look at the results.

Name IsClass IsValueType UnderlyingSystemType BaseType
Object True False System.Object 
ValueType True False System.ValueType System.Object 
Void False True System.Void System.ValueType
Boolean False True System.Boolean System.ValueType
Byte False True System.Byte System.ValueType
Int32 False True System.Int32 System.ValueType
Int64 False True System.Int64 System.ValueType
Single False True System.Single System.ValueType
Double False True System.Double System.ValueType
Decimal False True System.Decimal System.ValueType
Guid False True System.Guid System.ValueType
Array True False System.Array System.Object 
String True False System.String System.Object 
Hashtable True False System.Collections.Hashtable System.Object 
Random True False System.Random System.Object 
Regex True False System.Text.RegularExpressions.Regex System.Object 
PSObject True False System.Management.Automation.PSObject System.Object 
XmlDocument True False System.Xml.XmlDocument System.Xml.XmlNode 
ManagementObject True False System.Management.ManagementObject System.Management.ManagementBaseObject 
ManagementClass True False System.Management.ManagementClass System.Management.ManagementBaseObject 
DirectoryEntry True False System.PSObject System.Object 
Process True False System.PSObject System.Object 
ServiceController True False System.PSObject System.Object 
FileInfo True False System.PSObject System.Object 
DirectoryInfo True False System.PSObject System.Object