Data Types
When
you use the assignment operator = to set a variable to a specified
value, Windows PowerShell automatically assigns the best-suited data
type for the given value. In our first example, we used a simple string
as input, which stored an object of the type System.String in a variable. In this example, we set a variable to a numeric value:
We can use the GetType() method with the FullName property to find out the variable’s data type.
PS > $int.GetType().FullName
System.Int32
This shows that the variable $int is of the type System.Int32, which represents a 32-bit integer.
If we use a number that is too large for a 32-bit integer, a different data type will be used.
PS > $int64 = 10000000000000
PS > $int64.GetType().FullName
System.Int64
If the value is a decimal number, the System.Double data type will be used.
PS > $decimal = 1.2
PS > $decimal.GetType().FullName
System.Double
Rather than letting Windows PowerShell assign the
data type, you can specify the type for a variable. To assign a
specific data type to a variable, enclose the data type’s name in
square brackets before the variable name. If the data type is not at
the root of the System namespace, you must type the data type’s full name; otherwise, you can omit the System part of the name, as shown in this example:
PS > [uri]$url = "http://SPServer01"
PS > $url.GetType().FullName
System.Uri
Here, we use an URL as value, but rather than
letting Windows PowerShell assign a data type we assigned it a specific
data type, giving us a completely different type of object. The type System.Uri
is an object representation of a uniform resource identifier (URI),
which, according to Microsoft Developer Network (MSDN), is “a compact
representation of a resource available to your application on the
intranet or Internet.”
The next example demonstrates how to assign the type System.Int32 to a variable.
PS > [int32]$val = 32
PS > $val
32
Since
we assigned a fixed type to the variable, only values within the
permitted range for the type are allowed. If we try to add a string
value to the typed variable, an error occurs.
PS > [int32]$val = "http://SPServer01"
Cannot convert value "http://SPServer01" to type "System.Int32". Error: "Input
string was not in a correct format."
At line:1 char:12
+ [int32]$val <<<< = "http://SPServer01"
+ CategoryInfo : MetadataError: (:) [],
ArgumentTransformationMetadataException
+ FullyQualifiedErrorId : RuntimeException
Windows PowerShell also supports a number of type accelerators (also known as type shortcuts). The type accelerators allow you to use short name syntax for commonly used .NET types. For instance, instead of typing [System.Xml.XmlDocument], you can simply type [xml]. Table 1 shows some of the most common type accelerators.
Table 1. Common Windows PowerShell Type Accelerators
Type | Description | Example |
---|
[string] | String of Unicode characters | [string]"Hi" |
[int] | 32-bit integer | [int32]12 |
[long] | 64-bit integer | [int64]1200000 |
[char] | Unicode character | [char]34 |
[bool] | True or false value | [bool]$false |
[byte] | 8-bit integer | [byte]255 |
[decimal] | Decimal number | [decimal]12.44 |
[double] | Double-precision decimal number | [double]12.44 |
[float] | Single-precision floating number | [float]12.44 |
[array] | An array | [array]1,2,3 |
[hashtable] | Hashtable | [hashtable]@{"Name"="Value"} |
[xml] | XML document | [xml]"<name>Sergey</name>" |
[adsi] | Active Directory service interface | [adsi] "LDAP://DC=PowerShell,DC=nu" |
[wmi] | Type accelerator for ManagementObject | [wmi]("Win32_ComputerSystem.Name='SPServer01'") |
To find out the type accelerator’s corresponding .NET type, use the FullName property.
PS > ([string]).FullName
System.String
PS > ([xml]).FullName
System.Xml.XmlDocument
PS > ([adsi]).FullName
System.DirectoryServices.DirectoryEntry
Note
Type accelerators are considered before
regular type lookup. Regular type lookup searches for the name typed
within square brackets without prepending System. If that fails, System is prepended. For accelerated types in the System namespace, the accelerator ensures that you won’t pick up some global (not in any namespace) type.