Powershell Tutorial Online

Powershell DataTypes

Powershell Has Dynamic Types
Powershell has dynamic types. This means that the same variable can be used as different types:
Powershell allows the declaration of both implicitly-typed and explicitly-typed variables.
Implicitly typed variables may hold data of any type, and are declared without a type, as shown:
$myDataVar = "Hello, World!"
In this case, the data type is string, which means it represents a sequence of characters. Powershell data types can be more than strings, however. In fact, Powershell can use any data type defined by the Microsoft .NET Framework. The following declaration is also implicit, but stores a value of type double.
$myDataVar = 9.87
Since $myDataVar is an implicit type, it will hold "Hello, World!" as a string, and then when you assign 9.87 to it, $myDataVar will change to a double.
Powershell will automatically pick the correct data type based on the value provided.
There may be times when you will want to define exactly what type of data your variable holds. For instance, you may want to store the value 9.87 as a float instead of a double.
There are two ways to do this: to declare the type of the variable itself, or to declare the type of the variable's data.

Powershell with Strings

A string is a variable which stores a series of characters like "Robert William".
A string can be any text inside quotes. You can use single or double quotes:

Powershell with Numbers

Powershell Array
A PowerShell array holds a list of data items.
Two types of arrays
1. Not Declared Type
2. Strongly Typed
The data elements of a PowerShell array need not be of the same type, unless the data type is declared (strongly typed).
Create Arrays
To create an Array separate each elements with commas.
Create an array named $myFirstArray containing elements with a mix of data types:
$myFirstArray = 9,"Hello","MyData",8.5,"Robert"
or using explicit syntax:
$myFirstArray = @(9,"Hello",8.5,"Robert")
To distribute the values back into individual variables:
Create an array containing several numeric (int) values:
$myIntArray = 1,2,3,4,5,6,7
or using the range operator (..) for sequence.
$myIntArray = (1..7)
Here Strongly typed Array:
[int[]] $myTypedArray = 12,64,8,64,12
Create an empty array:
$myFirstArray = @()
Create an array with a single element:
$myFirstArray = @("Hello World")
Create a Multi-dimensional array:
$myMultiArray = @( (1,2,3), (10,20,30) )
Add values to an Array.
This is done using the old C language way '+=' operator
Append an extra element with a value of 23 to the $anyValue array:
$anyValue += 32
Retrieve items from an Array
To retrieve an element, specify its number, PowerShell numbers the array elements starting at 0.
Return all the elements in an array:
Return the first element in an array:
Return the seventh element in an array:
Return the 5th element through to the 10th element in an array:
Return the last element in an array:
Return the first element from the first row in a multi-dimensional array:
You can also combine named elements with a range, separating them with a +
so $myFirstArray[1,2+4..9] will return the elements at indices 1 and 2 and then 4 through 9 inclusive.
Return the length of an array (how many items are in the array):
Loop through the elements in an array:
foreach ($element in $myFirstArray) {$element}
When you create an array without specifying a datatype, PowerShell will create the array as an object array.
To determine the data type of an array:
If you pipe an array to Get-Member, it will display information about the objects in the array. If you use Get-Member -InputObject, that will display information about the array itself:
get-member -inputobject $myFirstArray
Set values
To change values in an array after it has been created, use the assignment operator (=) to specify a new value.
Alternatively use the SetValue method:
Add one array to another. This creates a new array containing all the values from the first two arrays:
$firstFive=@(2, 3, 4, 5, 6) $secondFive=@(7, 8, 9, 10, 11, 12) $all = $firstFive + $secondFive
Delete an Array (by deleting the array variable)
$firstFive=@(2, 3, 4, 5, 6)
Remove-Item variable:firstFive
Please do not use '$' symbol infront of variable.