programming4us
programming4us
WEBSITE

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

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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
Free Mobile And Desktop Apps For Accessing Restricted Websites
MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
TOYOTA CAMRY 2; 2.5 : Camry now more comely
KIA SORENTO 2.2CRDi : Fuel-sipping slugger
How To Setup, Password Protect & Encrypt Wireless Internet Connection
Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
Backup & Restore Game Progress From Any Game With SaveGameProgress
Generate A Facebook Timeline Cover Using A Free App
New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
- Messages forwarded by Outlook rule go nowhere
- Create and Deploy Windows 7 Image
- How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
- Creating and using an unencrypted cookie in ASP.NET
- Directories
- Poor Performance on Sharepoint 2010 Server
- SBS 2008 ~ The e-mail alias already exists...
- Public to Private IP - DNS Changes
- Send Email from Winform application
- How to create a .mdb file from ms sql server database.......
programming4us programming4us
programming4us
 
 
programming4us