Subscribe to Windows IT Pro
February 23, 2009 12:00 AM

Working with PowerShell's Data Types

Lesson 4 in the PowerShell 201 series explores how to work with strings, arrays, and other types of data
Windows IT Pro
InstantDoc ID #101150
Rating: (0)
Casting 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:

$a = [double] "10" 
$a.GetType().FullName 

Notice that you specify the Double type before the value. As Figure 6 shows, you can achieve the same results with the code

[double] $b = "10" 
$b.GetType().FullName 

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:

$a = "ten"; $a 
$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

$c = [double\[]] ("1","2","3") 
$c.GetType().FullName 

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:

[double\[]] $d = ("1","2","3") 
$d.GetType().FullName 

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

$c\[2] = "ten" 
$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 = Get-Date 
$e.GetType().FullName 
$e = [string] $e 
$e.GetType().FullName 

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 = [double\[]] ("1","2","3") 
$f.GetType().FullName 
$f = [string\[]] $f 
$f.GetType().FullName 

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 = 10, "ten", (Get-Date) 
$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.

 

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.