Executive Summary:
Windows PowerShell uses the Microsoft .NET Framework data types. Learn how PowerShell assigns and converts the System.String, System.Int32, System.Int64, System.Double, System.Decimal, System.Object\[], and System.Collections.Hashtable data types. Also learn how to cast and convert data to specific data types when needed. |
In Windows PowerShell, the data your statements retrieve, pass to other statements, and output to the console must conform to the Microsoft .NET Framework data types. For the most part, PowerShell automatically assigns and converts data to its correct data type. However, at times, you might want to control this process to create statements that better use the data you have available. Let's look at how to work with several types of data and how to cast and convert data to specific data types when needed.
Working with Strings
Administrators often work with data that's in the form of strings, and working with strings is a straightforward process in PowerShell. For example, the following code assigns the string "cat" to the $a variable, then displays the result, which is cat:
$a = "cat"; $a
Because the code assigns a string to $a, PowerShell automatically casts $a with the System.String type. You can use the GetType method and its FullName property to determine a variable's data type. For example, the command
$a.GetType().FullName
returns the result System.String. To add strings together, you use the plus (+) operator. For instance, the following code adds the string " & dog " (including spaces) to $a, then displays the result, which is cat & dog:
$a = $a + " & dog "; $a
You can add a numerical value to a string. When you do this, the number is automatically converted to a string. For example, the following code adds 10 to $a, then displays the result, which is cat & dog 10:
$a = $a + 10; $a
If you were to verify the data type, it would still be String. For more information about working with strings, see "PowerShell 101, Lesson 4."
Working with NumbersLike string data, numerical data is simple to work with in PowerShell. You assign numerical values as you would string values, except that you don’t enclose numerical values in quotes. When you specify a numerical value, PowerShell automatically assigns one of four data types to that value (unless you’re concatenating the numerical value to a string, as you saw in the previous example):
- System.Int32, which is a 32-bit integer ([int] alias).
- System.Int64, which is a 64-bit integer ([long] alias).
- System.Double, which is an 8-byte, floating-point decimal ([double] alias).
- System.Decimal, which is a 12-byte decimal ([decimal] alias). Decimal provides more precision than Double.
The following statements demonstrate how PowerShell assigns data types to numerical values:
$a = 1234; $a
$a.GetType().FullName
$a = 12345678910; $a
$a.GetType().FullName
$a = 1234.5678; $a
$a.GetType().FullName
$a = 1234.5678d; $a
$a.GetType().FullName
As Figure 1 shows, PowerShell assigns a data type to the first three variables based on the value.

However, to assign the Decimal type, you must specify the letter d directly after the number; otherwise, PowerShell treats the value as a Double type. You can use the plus operator to join numerical values. When you do so, the numerical values are added, not concatenated like they are with string values. For example, the following statements assign 1234 to $a, then add 1.5678 to $a:
$a = 1234; $a
$a.GetType().FullName
$a = $a + 1.5678; $a
$a.GetType().FullName
As Figure 2 shows, PowerShell adds the two values together and automatically converts the variable’s data type from Int32 to Double.

If you try to add a string that isn't a number to a numerical value, you'll receive an error. PowerShell can't convert nonnumeric string values into numerical values. For example, you can add the string "4" to a numerical value, but you can't add "four".