PowerShell Arrays

Variables can also store arrays which by definition are an indexed list of values. Each item is assigned a unique index number. The first item in an array is assigned the value of (0), the next item (1), then (2), and so on. One of the more popular uses of an array is to run a script against remote computers. If you've looked at the PowerShell scripts in the Microsoft Script Center Repository, notice that a lot of scripts only run on the local computer by using strComputer = "." variable. "." means the local computer the script is running on. You could run the script on a remote computer by modifying the code like this strComputer = "RemoteComputerName". But, what if you want to run the script against multiple computers on your network? Ok. besides using a logon script! In essence a logon script still runs locally and you have limited control over when it runs. The answer is by using an array!

To create an array, we create a variable and assign the array. Arrays are noted by the "@" symbol. Let's take the discussion above and use an array to connect to multiple remote computers:

$strComputers = @("Server1", "Server2", "Server3")<enter>

We now have three values stored in the $strComputers variable. Note: Since I am working with string values, I'm quoting. Also each array value is separated by a comma.

How to list the values in the array:

$strComputers<enter>

List the number of items within the array using the count property.

$strComputers.Count<enter>

Now that we know there are three elements within the array, we can list an item by it's index number. Remember, array index numbers start at 0. List values by their index number:

$strComputers[0]<enter>
$strComputers[1]<enter>
$strComputers[2]<enter>

We can also modify elements in an array. Here I'm modifying an element in an existing array by providing the index number and the new string value. This changes the item Server3 to Server4.

$strComputers[2] = "Server4"<enter>

Verify the element was modified:

$strComputers<enter>

Using the + operator, we can combine arrays:

$x = @(1, 2, 3, 4, 5)<enter>
$y = @(6, 7, 8, 9, 10)<enter>
$z = $x + $y<enter>
$z<enter>

Back to our discussion on using arrays to get information from multiple computers. Here is a typical script taken from the Microsoft Script Repository that enumerates the BIOS information on the (strComputer = ".") local computer. Note: you don't have to copy the following script code into a .ps1 (PowerShell) script file to run. Just copy and paste the code into the PowerShell Console; to paste in PowerShell just do a right mouse click. When the code has been copied into the shell, just hit <enter> twice to get the results. Just another cool thing you can do with PowerShell.

$strComputer = "."

$colItems = get-wmiobject -class Win32_BIOS -namespace root\CIMV2 -comp $strComputer

foreach ($objItem in $colItems) {
write-host "BIOS Characteristics: " $objItem.BiosCharacteristics
write-host "BIOS Version: " $objItem.BIOSVersion
write-host "Build Number: " $objItem.BuildNumber
write-host "Caption: " $objItem.Caption
write-host "Code Set: " $objItem.CodeSet
write-host "Current Language: " $objItem.CurrentLanguage
write-host "Description: " $objItem.Description
write-host "Identification Code: " $objItem.IdentificationCode
write-host "Installable Languages: " $objItem.InstallableLanguages
write-host "Installation Date: " $objItem.InstallDate
write-host "Language Edition: " $objItem.LanguageEdition
write-host "List Of Languages: " $objItem.ListOfLanguages
write-host "Manufacturer: " $objItem.Manufacturer
write-host "Name: " $objItem.Name
write-host "Other Target Operating System: " $objItem.OtherTargetOS
write-host "Primary BIOS: " $objItem.PrimaryBIOS
write-host "Release Date: " $objItem.ReleaseDate
write-host "Serial Number: " $objItem.SerialNumber
write-host "SMBIOS BIOS Version: " $objItem.SMBIOSBIOSVersion
write-host "SMBIOS Major Version: " $objItem.SMBIOSMajorVersion
write-host "SMBIOS Minor Version: " $objItem.SMBIOSMinorVersion
write-host "SMBIOS Present: " $objItem.SMBIOSPresent
write-host "Software Element ID: " $objItem.SoftwareElementID
write-host "Software Element State: " $objItem.SoftwareElementState
write-host "Status: " $objItem.Status
write-host "Target Operating System: " $objItem.TargetOperatingSystem
write-host "Version: " $objItem.Version
write-host
}

Now we want the same information from multiple remote computers. This time you choose which ones. Create an array with two (or more) computers, FYI - you must have local admin rights on each computer to run the script. Use notepad and change the computer names in the array. Copy and paste your new code into PowerShell.

$strComputer = @("computer1", "computer2", "computer3")

$colItems = get-wmiobject -class Win32_BIOS -namespace root\CIMV2 -comp $strComputer

foreach ($objItem in $colItems) {
write-host "BIOS Characteristics: " $objItem.BiosCharacteristics
write-host "BIOS Version: " $objItem.BIOSVersion
write-host "Build Number: " $objItem.BuildNumber
write-host "Caption: " $objItem.Caption
write-host "Code Set: " $objItem.CodeSet
write-host "Current Language: " $objItem.CurrentLanguage
write-host "Description: " $objItem.Description
write-host "Identification Code: " $objItem.IdentificationCode
write-host "Installable Languages: " $objItem.InstallableLanguages
write-host "Installation Date: " $objItem.InstallDate
write-host "Language Edition: " $objItem.LanguageEdition
write-host "List Of Languages: " $objItem.ListOfLanguages
write-host "Manufacturer: " $objItem.Manufacturer
write-host "Name: " $objItem.Name
write-host "Other Target Operating System: " $objItem.OtherTargetOS
write-host "Primary BIOS: " $objItem.PrimaryBIOS
write-host "Release Date: " $objItem.ReleaseDate
write-host "Serial Number: " $objItem.SerialNumber
write-host "SMBIOS BIOS Version: " $objItem.SMBIOSBIOSVersion
write-host "SMBIOS Major Version: " $objItem.SMBIOSMajorVersion
write-host "SMBIOS Minor Version: " $objItem.SMBIOSMinorVersion
write-host "SMBIOS Present: " $objItem.SMBIOSPresent
write-host "Software Element ID: " $objItem.SoftwareElementID
write-host "Software Element State: " $objItem.SoftwareElementState
write-host "Status: " $objItem.Status
write-host "Target Operating System: " $objItem.TargetOperatingSystem
write-host "Version: " $objItem.Version
write-host
}

For more information on building arrays to enumerate remote computers, here is a very cool article I've written: 3 easy steps to get information from remote computers .

Powershell Array, Hashes

Powershell is worked on .net framework.
So a PowerShell array is your .NET System.Array.
PowerShell makeit simpler.
It is dead simple to create arrays in PowerShell: separate the items with commas, and if they are text, wrap them in quotes.

Example

    PS C:> $language = "C#", "VB", "Perl", "Java"
    PS C:> $language.GetType()
    ---OutPut------
    IsPublic IsSerial Name     BaseType
    -------- -------- ----     --------
    True     True     Object[] System.Array
    
    PS C:> $language
    ---OutPut------
    C#
    VB
    Perl
    Java
   

Copy and Try it

Powershell Multidimensional Arrays

Example

    $arr=@{}
    $arr["david"] = @{}
    $arr["david"]["TSHIRTS"] = @{}
    $arr["david"]["TSHIRTS"]["SIZE"] ="M"
    $arr.david.tshirts.size
    

Copy and Try it