Difference between revisions of "Basic Windows PowerShell 1.0 Types"

From Techotopia
Jump to: navigation, search
Line 1: Line 1:
 
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#.
 
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.
  
 
== PowerShell Numeric Types ==
 
== PowerShell Numeric Types ==

Revision as of 20:52, 18 November 2008

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

A type may 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

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