PS
Powershell Tutorial Online

PowerShell Array Hashes Variable

What is array? An array is a number of items arranged in some specified way. Like in other languages we can define variable as array and assign or add items or values in it. In PowerShell defining array is very simple and flexible. You just have items separated by the by the comma.
We will see some examples of array also we will how to calculate length of the array.
We will see some examples to find array contain and indexing syntax.
How to the array and last but not least example of type cast the array element into data type to maintain the type safety

All arrays in PowerShell are 0 based, that means PowerShell array start with zero. (It is similar for most of the languages)

Example 1: Declare array simple array


# Learn-Powershell-Array-Example-1
# Create simple array of strings
$myArray = "abc", "xyz"
$myArray

Example 2: Retrieve specific array element
All arrays in PowerShell are 0 based, To Retrieve any element or item you have to give index -1 in square brackets e.g. $myArray[1]


# Learn-Powershell-Array-Example-1
# Create simple array of strings
$myArray = "abc", "xyz"
# Retrieve Second ellement of array (Array is zero based)
$myArray[1]

Example 3: Retrieve all array elements # Learn-Powershell-Array-Example-1 # Create simple array of strings $myArray = "abc", "xyz", "pqr" $myArray

Example 4: Get data type of array
$myArray.GetType()


# Learn-Powershell-Array-Example-1
# Create simple array of strings
$myArray = "abc", "xyz"
# Get type of array
$myArray.GetType()


Example 5: Update Specific Element of Array
To update any specific element of array just specify the array variable with index in square bracket and assign value with equal operator.


# learn-powershell-array-examples
# create simple array of strings
$myarray="abc" , "xyz" , "pqr" , "dcb"
# before
$myarray
# update of array
$myarray[2]="new"
# after
write-host "############### After##########"
$myarray


Example 6: Updating Whole Array
To update whole array of elements

 
# Learn-Powershell-Array-Examples
# Create simple array of strings
$myArray = "abc", "xyz", "pqr", "dcb"
# Before
$myArray
# update of array
$myArray[2] = "new", "value"
# After
Write-Host "############### After##########"
$myArray

Example 7: Another way of creating Array


# Learn-Powershell-Array-Examples
# Create simple array of strings
$myArray = @("abc", "xyz", "pqr", "dcb")

$myArray


Example 8: Create empty array in PowerShell, using this way we can create empty array


    # Learn-Powershell-Array-Examples
    # Create simple array of strings
    $myArray = @()
    $myArray.Length
        

Example 9: Shortcut to create Array (Put the first value followed by two dots/periods and then last value)
You can specify any range here, Although we have create array with 1, but you can start with any number.
Powershell is smart enough to create array if you provide first value greater than second, it create an array of reverse values.


# Learn-Powershell-Array-Examples
# Creating array usinshortcut
$myArray = 1..8
$myArray




Example 10: Check contains of Array Check if values is present becareful type of array, here it is int array so dont specify value with double quotes

# Learn-Powershell-Array-Examples
# Creating array using shortcut
$myArray = 14..8
$myArray
Write-Host
# check if values is present becareful type of array, here it is int array so dont specify value with double quotes
$myArray.Contains(11)

Example 11: Check notcontains of Array (We should use of of -notcontains as it sometimes confuse users) PS C:\> $myarray = 5, 10, 20, 4 $myarray -notcontains 20

Example 12: Get the length of Array

    
# Learn-Powershell-Array-Examples
# Create simple array of strings
$myArray = "abc", "xyz", "pqr", "dcb"
# Before
$myArray
Write-Host "Length of array"
$myArray.Length
    

Example 13: Sort Array elements ascending order

Example 14: Sort Array elements descending order

Example 15: Array Decimal elements


ArrayList

Adding and removing Items from a Power Shell array is a topic which can teach you how it is related to c# System.Array Object Create an power shell array and we will note the type System.Array


$Items = "item1","item2","item3","item4"
$Items.GetType()
However, if we try to Add or Remove items to the array we get errors that the "Collection was of a fixed size"

$Items.Add("item1")
$Items.Remove("item2")
$Items.IsFixedSize
By Default array we originally created is of a fixed size. It only allows you to modify of existing elements, but not the addition or removal of others. One way to manage with this is to use of type System.Collections.ArrayList instead of Array

[System.Collections.ArrayList]$ArrayList = $Items
$ArrayList.GetType()
Now try to do following operations.

$ArrayList.Add("item5")
$ArrayList
$ArrayList.Remove("item3")
$ArrayList

-------Hash Table-------------

PowerShell has another method for holding lists of values called hash tables.
What is Hash Table
A hash table is a data structure used to implement an associative array, a structure that can map keys-values pairs.
From which the correct value can be found using key instead of index we saw in above example.
In short array is numerically based and hash table is key based collection

Example 1: Decalre/Create Hash table

Example 2: Get all the vales of Hash table

Example 3: Get single value from Hash table using key

Example 4: Get single value from Hash table using object type syntax

Example 5: Get single value from Hash table using variables $hash.$myKey

Example 6: Get single value from Hash table using variables another way $hash.$($myKey)

Example 7: Add/Replace or remove values Hash table $hash["aaa"] = "Anything"

Example 8: Remove values Hash table $hash.Remove("aaa")

Example 9: Contains in Hash table $hash.Remove("aaa")

Example 10: Get all keys in Hash table $hash.Keys

Example 10: Get all values in Hash table $hash.Values


-----Variables

Built in variables in PowerShell.

[string] Fixed-length string of Unicode characters
[char] A Unicode 16-bit character
[byte] An 8-bit unsigned character
[bool] Boolean True/False value
[int] 32-bit signed integer
[single] Single-precision 32-bit floating point number
[double] Double-precision 64-bit floating point number
[long] 64-bit signed integer
[DateTime] Date and Time
[decimal] A 128-bit decimal value
[array] An array of values
[hashtable] Hashtable object
[xml] Xml object