programming4us
programming4us
WEBSITE

PowerShell for Microsoft SharePoint 2010 : Variables in Windows PowerShell (part 2) - Data Types

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
1/22/2014 11:19:34 PM

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:

PS > $int = 10

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
TypeDescriptionExample
[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.

Other  
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
programming4us
 
 
programming4us