Different Data Types
Now let’s take a look at some other considerations
when using user-defined variables.
But first, create $var1 again with the
statement
$var1 = "one two"
because
you just used the
Remove-Variable cmdlet to
delete it. As with built-in variable
values, user-defined variable
values can be retrieved by
entering the variable’s name:
$var1
Because you assigned a string
value to $var1, PowerShell stores the value
as a string object. To verify the object type,
you can use the GetType() method:
$var1.gettype()
Figure 2 shows the results of all three of
these statements.
You’re not limited to string values when
you create a variable. You can just as easily
assign an integer:
$var1 = 123; $var1.gettype()
PowerShell automatically stores the value
as the correct type, which is Int32, as Figure
3 shows. Notice that this code includes
multiple statements. As I discussed in Lesson
2, you can use a semicolon to manually
terminate a statement. So, provided you
use semicolons, you can put multiple statements
on one line. In this case, I’ve
included two statements: the first
assigns the numeric value, and the
second retrieves its type.
Also note that this code assigns
a value to the same variable used in
the previous set of examples. I did
this to demonstrate that the process
of creating and updating a variable
is the same.
Besides strings and integers, you
can assign other types of values,
such as
$var1 = 12.34; $var1.gettype()
$var1 = get-date; $var1.gettype()
The first line stores the value as
type Double, whereas the second
line stores the value as type Date-
Time, as Figure 3 shows.
To append text to an existing
string value in a user-defined
variable, you can follow the same
approach I outlined for appending text to an existing string value in an environment
variable. For example, the following
code assigns a string value to $var1, then
appends the string four to it:
$var1 = "one two three"; $var1
$var1 = $var1 + " four"; $var1
Figure 4 shows the results.
Now let’s try a similar operation with
numerical values:
$var1 = 123; $var1
$var1 = $var1 + 4; $var1
In this case, PowerShell doesn’t append the
number 4, but instead adds 4 to the total
amount, as shown in Figure 4. You can,
however, define the number as a string by
enclosing it in quotes:
$var1 = "123"; $var1
$var1 = $var1 + 4; $var1
The number 4 is now appended to the
original value. In fact, you can take this
approach with any string:
$var1 = "one two three"; $var1
$var1 = $var1 + 4; $var1
In this case, the results shown in Figure 4 might seem odd, but PowerShell has done
exactly what you’ve told it to do.
Although you can append text or a
number to a string value, you can’t append
text to a numeric value. For example, if you
attempt to append the string four to the
number 123
$var1 = 123; $var1
$var1 = $var1 + "four"; $var1
you’ll receive an error, as seen in Figure
4. You can append values to an existing
value only when the types are compatible.
The important point to remember is
that PowerShell tries to do the right thing
by letting you assign any type of data to a
variable as long as it can be converted to
the correct type.
Continue on Page 4