Using arrays and hash tables

In PowerCLI, you can create a list of objects. For example, red, white, and blue is a list of strings. In PowerShell, a list of terms is named an array. An array can have zero or more objects. You can create an empty array and assign it to a variable:

PowerCLI C:\> $Array = @()

You can fill the array during creation using the following command line:

PowerCLI C:\> $Array = @("red","white")

You can use the += operator to add an element to an array:

PowerCLI C:\> $Array += "blue"
PowerCLI C:\> $Array
red
white
blue

If you want to retrieve a specific element of an array, you can use an index starting with 0 for the first element, 1 for the second element, and so on. If you want to retrieve an element from the tail of the ...

Get Learning PowerCLI - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.