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
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 Numbers
Like 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 = 12345678910; $a
$a = 1234.5678; $a
$a = 1234.5678d; $a
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 = $a + 1.5678; $a
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".Working with Arrays
Arrays are collections that have the System.Object\[\] type. The \[\] after the data type lets you know that it's an array with multiple values. PowerShell automatically assigns the Object\[\] type to arrays. For instance, the code
$a = "a","b","c"; $a
creates an array with three string values, then returns the results
You use an array's index number to access the individual values in the array. Arrays use a 0-based index, so to access the first value, you specify the array's name followed by the index number in brackets, as in
This statement returns the result a. If you want to access more than one value, you just include as many statements as you need. For example, the statements
return the results
You can use the plus operator to add a value to an array. For instance, the code
$a = $a + "d"; $a
adds d to the $a array, then displays the results
When d is added to the array, PowerShell assigns the next available index number (3) to that value. You can assign numerical values to an array as easily as you assign string values. For example, the command
$a = 1,2,3; $a
returns the results
Once again, PowerShell assigns the Object\[\] type to the array. You can include any type of numerical value in an array. For example, the following code assigns an integer value (1) and two decimal values (2.2 and 3.33) to $a:
$a = 1,2.2,3.33; $a
In fact, you can add any type of value to an array. For instance, the following code creates an array that includes a numerical value (10), a string value ("cat"), and a date-time value (the current date and time, which is obtained with the Get-Date cmdlet):
As Figure 3 shows, the array type is Object\[\].
However, the data types of the individual values remain specific to those values. You can return the individual values' types with statements such as
Each statement uses the index number to identify the value, then uses GetType to retrieve the value's type. Figure 3 shows the results. To change a value in an array, you specify the value's index number and assign the new value, as in
PowerShell replaces the value and assigns the correct type to that value, as shown in Figure 3.Working with Hash Tables
Hash tables are collections that take the System.Collections.Hashtable data type. The primary difference between hash tables and arrays is that hash tables use named keys rather than index numbers to identify values. For example, the following code assigns three keys and their values to the $a hash table, then displays that table's contents and data type:
As the first line shows, you create a hash table by using the @ symbol followed by a set of braces that enclose the collection of key/value pairs. The first key/value pair is b/bird, as Figure 4 shows.
You can retrieve a list of the keys in a hash table by using the Keys property. For example, the statement
$a.Keys | sort
retrieves the $a hash table's keys, then uses the Sort-Object cmdlet (referenced with the sort alias) to sort the keys alphabetically. Similarly, you can use the Values property to retrieve a hash table's values, as this statement shows:
$a.Values | sort
You can also retrieve an individual value by referencing the key, with a statement such as
Figure 4 shows the results of these last three statements. To add a key/value pair to a hash table, you can reference the new key as you would an existing key, then provide the new key's value, as in
$a.e = "elephant"; $a
As Figure 5 shows, the key/value pair of e/elephant is added.
To change the value of an existing key, you reference the key and provide the new value, as in
$a.d = "dingo"; $a
This code changes the d/dog pair to d/dingo. To remove a key/value pair, you use the Remove method. For example, to remove the d/dingo pair, you'd use code such as
$a.remove("d"); $aCasting and Converting Data Types
At times, you might want to control the data type assigned to a scalar (i.e., single) value. For example, suppose you need to make sure the value of 10 is treated as a numerical value and not a string. You can use the following code to create a variable that holds a Double-type value, even though the initial value is a string:
Notice that you specify the Double type before the value. As Figure 6 shows, you can achieve the same results with the code
However, there's a difference between the two approaches. The first approach simply changes the value to the Double type. The second approach strongly types the variable, which means you can assign only values with the same data type to that variable. You can test this difference by trying to assign a string to the $a and $b variables:
$b = "ten"
As Figure 6 shows, you can assign a string to $a but not $b.
You can also control the data type assigned to an array. For example, the code
assigns the Double\[\] type to the $c array, overriding the default Object\[\] type. The \[\] denotes it's still an array with multiple values. As Figure 7 shows, the following code achieves the same results:
Unlike scalar values, arrays are strongly typed by both approaches. For example, if you try to change one of the values to a string with the statements
$d\[2\] = "ten"
the statements will fail (see Figure 7). There might be times when you need to convert an existing variable's data type. For instance, suppose you need to convert a date-time variable to a string variable. You can use code such as
$e = \[string\] $e
In this code, the $e variable initially contains the value returned by the Get-Date cmdlet. The code then converts the variable to the String type and reassigns the value back to the variable. As Figure 8 shows, PowerShell changed the variable to the String type.
You can take a similar approach with an array. The following code creates a Double\[\]-type array, converts the array to the String\[\] type, and reassigns the values back to the array:
$f = \[string\[\]\] $f
As Figure 8 shows, the variable's data type is now String\[\]. Even if an array's values are different data types, you can convert them all to one type with code such as
$g = \[string\[\]\] $g
When you convert the array's data type to String\[\], PowerShell converts all the values to strings.Let PowerShell Do the Work—Or Not
As you've seen in this lesson, PowerShell does most of the work when it comes to assigning and converting data types. However, you can cast and convert data to override this behavior in order to create statements that can better use the available data.