Basic Windows PowerShell 1.0 Types

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
Understanding and Creating Windows PowerShell 1.0 VariablesWorking with Arrays in Windows PowerShell 1.0


Purchase and download the full PDF version of this PowerShell eBook for only $8.99


Basic data types (also referred to as primitive types) are the key building blocks of any programming or scripting language. It is these types which allow numbers, strings and true or false values to be stored and manipulated. In the case of Windows PowerShell, these are based on the types provided by the .NET framework, providing a range of types which are not only rich and well documented, but also familiar to those who have used other languages such as Visual Basic, ASP.Net and C#.

This chapter covers Windows PowerShell numeric, boolean and string basic types. More complex types such as arrays and associative arrays (hashtables) will be covered in subsequent chapters.


Contents


PowerShell Numeric Types

Windows PowerShell 1.0 supports the full range of .Net number types as outlined in the following table:

.Net Type Name

Short Type Name

Example

System.Int32

[int]

20

System.Int64

long

20000000000

System.Double

[double]

2.1

System.Decimal

[decimal]

2d

System.Byte

[byte]

2

When using a number, it is not usually necessary to prefix the value with the type since PowerShell is generally able to ascertain the correct type based on the size and format of the value. If, for example, the value is too high for an Int32 an Int64 may be created. Similarly, if the number contains a fraction then a Decimal or Double type will be used.

It is possible to identify the type of a variable by using the gettype() method of the object. For example:

PS C:\Users\Administrator> $myval = 10.432
PS C:\Users\Administrator> $myval.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Double                                   System.ValueType

As illustrated in the above PowerShell console output, a variable assigned the value 10.432 is stored as type Double by Windows PowerShell. To obtain only the full .Net type name use gettype().fullname as follows:

PS C:\Users\Administrator> $myval.gettype().fullname
System.Int32

Alternatively, the short name may similarly be obtained using gettype().name as illustrated below:

PS C:\Users\Administrator> $myval.gettype().name
Int32

This technique may also be used to identify which type will be used for a value by enclosing the actual numeric value in question in parentheses and then using the gettype() as previously outlined:

PS C:\Users\Administrator> (9898.989).gettype().fullname
System.Double

Rather than having the type implied, the type may be also be explicitly set by prefixing the value with either the full or short type name. For example:

PS C:\Users\Administrator> [byte] $myval = 10
PS C:\Users\Administrator> $myval.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Byte                                     System.ValueType

In the case of explicitly making a value Decimal, the value may be followed by the d character:

PS C:\Users\Administrator>  $myvalue = 10d
PS C:\Users\Administrator> $myvalue.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Decimal                                  System.ValueType

Note that once the type has been explicitly set on a variable, it is not possible to implicitly change the variable type simply by assigning a value of a different type to the variable. Any attempt to do so will result in an error message from the shell. As a case in point, the following attempt to assign a string to a variable which has been explicitly declared as Int32 fails:

PS C:\Users\Administrator> [int32] $myvariable = 520
PS C:\Users\Administrator> $myvariable = "A String"
Cannot convert value "A String" to type "System.Int32". Error: "Input string was not in a correct f
ormat."
At line:1 char:12
+ $myvariable  <<<< = "A String"

This problem may, however, be overcome quite simply by explicitly declaring the new type of the variable when assigning the value:

PS C:\Users\Administrator> [string] $myvariable = "A String"

Specifying Hexadecimal Numbers

Hexadecimal numbers are specified in PowerShell using the same approach used by most other programming languages such as C, C++, C# and Java. The technique involves prefixing the number with 0x. For example:

PS C:\Users\Administrator> 0x1233fadf
305396447

PowerShell Boolean Type

Boolean values (which can be either 1 or 0) are defined in PowerShell using the .Net System.Boolean type (the short from of which is [bool]). For example, the following command assigns true to a variable of boolean type:

PS C:\Users\Administrator> [bool] $myval = 1
PS C:\Users\Administrator> $myval.gettype().fullname
System.Boolean

When working with boolean variables, the pre-defined $true and $false variables may also be used:

PS C:\Users\Administrator> [bool] $myval = $false
PS C:\Users\Administrator> $myval
False

PowerShell String Type

Strings in PowerShell are represented using the .Net System.String type. Strings may be single quoted or double quoted (details of which are covered in detail in the chapter entitled Windows PowerShell 1.0 String Quoting and Escape Sequences).

A here-doc or here-string (a string containing large amounts of text on multiple lines) can be created by encapsulating the string in @[Quote][newline] and "@[Quote][Newline] sequences, using either single or double quotes:

PS C:\Users\Administrator> $mystring = @'
>> This is a single
>> quoted here-string
>> in PowerShell
>> '@
>>
PS C:\Users\Administrator> $mystring
This is a single
quoted here-string
in PowerShell

PS C:\Users\Administrator> $mystring = @"
>> This is a double
>> quoted here-string
>> in PowerShell
>> "@
>>
PS C:\Users\Administrator> $mystring
This is a double
quoted here-string
in PowerShell

Windows PowerShell Type Casting

Clearly, PowerShell will make a judgment as to a value's type. Most of the time this is fine, but occasionally it is necessary to specifically specify the type. This is achieved using type casting. Type casting involves preceding the value or variable with the short or long variable name enclosed in square brackets. For example, to force variable $x to be double (as opposed to the default Int32):

PS C:\Users\Administrator> [double] $x = 10
PS C:\Users\Administrator> $x.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Double                                   System.ValueType

Casting can similarly be used to cast a variable that would clearly ordinarily be assumed by PowerShell to be an integer type as a string:

PS C:\Users\Administrator> [string] $mystring = 10
PS C:\Users\Administrator> $mystring.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

<google>BUY_WPS_BOTTOM</google>