Difference between revisions of "Basic Windows PowerShell 1.0 Types"

From Techotopia
Jump to: navigation, search
(New page: == PowerShell Numeric Types == Windows PowerShell 1.0 supports the full range of .Net number types as outlined in the following table: <table border="1" cellpadding="5" cellspacing="0" i...)
 
(PowerShell Numeric Types)
Line 91: Line 91:
 
</pre>
 
</pre>
  
As illustrated in the above PowerShell console output, a variable assigned the value 10.432 is stored as type ''Double'' by Windows PowerShell.
+
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:
 +
 
 +
<pre>
 +
PS C:\Users\Administrator> $myval.gettype().fullname
 +
System.Int32
 +
</pre>
 +
 
 +
A type may be explicitly set by prefixing the value with either the full or short type name. For example:
 +
 
 +
<pre>
 +
PS C:\Users\Administrator> [byte] $myval = 10
 +
PS C:\Users\Administrator> $myval.gettype()
 +
 
 +
IsPublic IsSerial Name                                    BaseType
 +
-------- -------- ----                                    --------
 +
True    True    Byte                                    System.ValueType
 +
</pre>
 +
 
 +
In the case of explicitly making a value ''Decimal'', the value may be followed by the ''d'' character:
 +
 
 +
<pre>
 +
PS C:\Users\Administrator>  $myvalue = 10d
 +
PS C:\Users\Administrator> $myvalue.gettype()
 +
 
 +
IsPublic IsSerial Name                                    BaseType
 +
-------- -------- ----                                    --------
 +
True    True    Decimal                                  System.ValueType
 +
</pre>
 +
 
 +
== 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:
 +
 
 +
<pre>
 +
PS C:\Users\Administrator> 0x1233fadf
 +
305396447
 +
</pre>
 +
 
 +
== 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:
 +
 
 +
<pre>
 +
PS C:\Users\Administrator> [bool] $myval = 1
 +
PS C:\Users\Administrator> $myval.gettype().fullname
 +
System.Boolean
 +
</pre>
 +
 
 +
When working with boolean variables, the pre-defined ''$true'' and ''$false'' variables may also be used:
 +
 
 +
<pre>
 +
PS C:\Users\Administrator> [bool] $myval = $false
 +
PS C:\Users\Administrator> $myval
 +
False
 +
</pre>
 +
 
 +
== 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:
 +
 
 +
<pre>
 +
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
 +
</pre>

Revision as of 20:30, 18 November 2008


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