Working with PowerShell Data types, PowerShell Variables
Windows PowerShell uses the Microsoft .NET Framework data types. Whenever you use PowerShell, whether it is querying data or making a change to some configuration or existing object, you are working with different types of objects. Everything that you query or work with is an object such as a string or a larger object with multiple properties that may have other nested objects within it.
PowerShell Data types:
- System.String
- System.Int32
- System.Int64
- System.Double
- System.Decimal
- System.Object[]
- System.Collections.Hashtable
We need not to define the type of variable. PowerShell automatically decides the type of variable. Variables are always specified with the initial character $, and can include any alphanumeric characters or the special characters in their names. Special Characters and spaces are not recommended as they are difficult to handle in scripts. We can use underscore ( _ ) instead.
$variable1, $variable2 … Variables are not case sensitive.
Working with Strings:
See the following examples. We are assigning a string to variable $a.
PS C:\> $a = "I am a string" PS C:\> $a.GetType().FullName System.String
PowerShell automatically assigned the variable type to System.String . Now let us play with strings.
PS C:> $a = "One" PS C:> $b = "Two" PS C:> $a + $b OneTwo PS C:> "$a$b" OneTwo PS C:> $a$b At line:1 char:3 + $a$b + ~~ Unexpected token '$b' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken
$a$b has thrown some error. We cannot use like this. If you want to print both the variables, use them in “”.
Now, what will happen if you use single quotes? (‘). Let us see..
PS C:\> '$a$b' $a$b
Single quote marks result in literal values being echoed back; double quote marks result in the actual value of a variable being echoed back.
Working with Numbers:
PS C:\> $a = 1234; $a 1234 PS C:\> $a.GetType().FullName System.Int32 PS C:\> $a = 12345678910; $a 12345678910 PS C:\> $a.GetType().FullName System.Int64 PS C:\> $a = 1234.5678; $a 1234.5678 PS C:\> $a.GetType().FullName System.Double PS C:\> $a=1234.5678d; $a 1234.5678 PS C:\> $a.GetType().FullName System.Decimal PS C:\>
Converting Data types:
PS C:\> $a = [double] "1234" PS C:\> $a.GetType().FullName System.Double PS C:\> $a = [decimal] "1234" PS C:\> $a.GetType().FullName System.Decimal
Working with Arrays:
A PowerShell array holds a list of data items.
Creating Arrays
To create an Array just separate the elements with commas or using explicit syntax:
PS C:\> $myArray = "Hello","World", "One", 1, 1.5, "Hello World" PS C:\> $myArray Hello World One 1 1.5 Hello World PS C:\> $myArray = @("Hello","World", "One", 1, 1.5, "Hello World") PS C:\> $myArray Hello World One 1 1.5 Hello World
range operator (..):
PS C:\> $myArray = (10..15) PS C:\> $myArray 10 11 12 13 14 15 PS C:\>
Create an empty array:
$myArray$ = @()
Add values to an Array. This is done using the += operator
$countries += 'India'
Retrieve items from an Array:
PS C:\> $myArray = "Hello","World", "One", 1, 1.5, "Hello World" PS C:\> $myArray[0] Hello PS C:\> $myArray[4] 1.5 PS C:\> $myArray[5] Hello World
Length of Array:
PS C:\> $myArray.Count 6
Searching array:
PS C:\> $arrColors = "blue", "red", "green", "yellow", "white", "pink", "orange", "turquoise" PS C:\> $arrColors -contains "black" False PS C:\> $arrColors -contains "orange" True
Working with Hash Tables:
Hash tables are collections that take the System.Collections.Hashtable data type. The primary difference between hash tables and arrays is that hash tables use named keys rather than index numbers to identify values.
Hash Tables are one of the most flexible datatypes supported in PowerShell.
We create a hash table by using the @ symbol followed by a set of braces that enclose the collection of key/value pairs.
PS C:\> $a = @{a="Birds"; b="Animals"; c="Mountains"; d="Oceans"} PS C:\> $a Name Value ---- ----- c Mountains d Oceans b Animals a Birds PS C:\> $a.a Birds PS C:\> $a.d Oceans PS C:\> $name = @{FirstName = "My FirstName"; LastName = "My LastName";} PS C:\> $name Name Value ---- ----- LastName My LastName FirstName My FirstName PS C:\> $name.FirstName My FirstName PS C:\> $name.LastName My LastName