WEBSITE

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

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
Review : Sigma 24mm f/1.4 DG HSM Art
Review : Canon EF11-24mm f/4L USM
Review : Creative Sound Blaster Roar 2
Review : Philips Fidelio M2L
Review : Alienware 17 - Dell's Alienware laptops
Review Smartwatch : Wellograph
Review : Xiaomi Redmi 2
Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
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)
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
Popular Tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8
Visit movie_stars's profile on Pinterest.