Windows PowerShell Array String

PowerShell Array @("value1", "value2")

Arrays strings are designed to store text data.  On this page I want exploit this capability by examining the use of $Variables with PowerShell’s parameters.


PowerShell Array Structure

PowerShell’s syntax is subtle, but strict.  Put another way, each type of bracket has different significance, and single or double quotes often produce different results.  In the case of PowerShell’s array the signature is the ‘at sign’ @.

Store Computer Names in an Array String

One of the most popular uses of an Array in PowerShell is for storing multiple values for a script’s parameters, for example: -ComputerName, or -Exclude.

Sooner or later you will want your script to ‘fan out’ and execute on multiple servers.  Once the basic script works, the technique is to store the multiple values for the hostname in an array, and assign it to a variable.

Let us use Get-Process -ComputerName as the vehicle to experiment with PowerShell’s arrays.

# An array to assign multiple computernames to a variable.
$Victims = @("LocalHost", "$env:COMPUTERNAME")
Get-Process -ComputerName $Victims | Select Name, MachineName

Note 1: Arrays usually contain more than two elements.  Also, for this experiment you would normally use the names of different computers!

Note 2: Observe the rhythm of the collection:

  • Declare an array with at sign: @(
  • Encase each element in "double quotes"
  • Use a comma, because each element needs a separator.
  • Sign off with a parenthesis style bracket).  {curly brackets mean a hash table}

Array For a Parameter With Multiple Values

Scenario: You want to exclude multiple file types.  The answer is create a collection which holds the values that you want to remove from the results list.

# List files in System32 -Exclude with @(array string)
$FileSource = "$env:SystemRoot\system32"
$Dross = @("*nls*", "*audio*", "*xa*")
$List = Get-ChildItem -Path $FileSource -Exclude $Dross
Write-Host "Number of files in System32: " $List.count

Challenge: Change the values of the array elements, and then check the number of files returned, for example change "*nls*" to "*dll*" and see the resulting file number drop.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Question: Does a Particular Parameter Support an Array String?

When I was researching Get-ChildItem I discovered that -Exclude and -Include supported an array, but -Filter did not.  What I learned was that you need to pay close attention to the Type property, for example:

-Filter <String>
-Exclude <String[]>  Square brackets indicate parameter will support an array.

Researching PowerShell Cmdlets

Here is one of my favourite techniques for finding cmdlets that support a particular parameter, for example -ComputerName.

# PowerShell script to research Computer parameters
Get-Command | Where { $_.parameters.keys -Contains "ComputerName"}

The Array in a PowerShell Loop

You may already realize that PowerShell uses arrays in loops.  However, it is easy to concentrate on the looping and not realise the role played by the array.

# PowerShell Foreach Array Example
Foreach ($number in 1..10 ) { $number * 13}

The array part is ($number in 1..10).  Looping is controlled by the Foreach command. Naturally, this is not an string array, fortunately for me the system deals with the Typename automatically.

SolarWinds Response Time Viewer for WiresharkGuy Recommends: Response Time Viewer for Wireshark

Here is a free tool to troubleshoot network connection and latency problems.  Key concept: this is a free tool from SolarWinds that analyzes network packets captured by Wireshark (also a free tool).

When you inspect the data in the Response Time Dashboard, if you hover over an application such as Teredo or TCP, then you get an orange box showing a breakdown of network and application response times, note the 'Peak value' in addition to the 'Average'.

Download your free trial of SolarWinds Response Time Viewer for Wireshark

Further Information About PowerShell’s Arrays

PowerShell supports a comprehensive series of ‘about’ documents; array is an easy filename to remember:

# Research About PowerShell Array
Get-Help about_Arrays -Full

When I read this help document, I discovered how to return one particular element from an array.

# Retrieve an element from a PowerShell array
$Victim = @("","","")

Challenge: Try ‘Help about_Arrays’, I bet you will find something new or interesting about this construction.

Array Research
Here is a little script to check if it’s true that a variable is an array, and if so to count how many elements are present.

# Check the elements in a PowerShell array string
$Victim = @("","","")
$Victim = "Guy"
If($Victim -Is [Array]) {Write-Host "The array has" $Victim.Count "elements"}
ElseIf($Victim -Isnot [Array]) {Write-Host "No array for this variable"}

Vital Note 3: Please remark out line 4,
# $Victim = "Guy"

Now the script will show the ‘If’ statement in action.

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

The Array’s Big Brother: Hash Table

Arrays are satisfactory when you have a series of single values.  Hash tables build on an arrays by permitting you to store data in the format of Key = Value.

$Orders = @{"Chaz" = "Pizza"; "Jane" = "Crab"; "Eddie" = "Meat Pie"; "Guy" = "Curry"}

Note 4: Hash tables use semi-colon; between each pair.

Note 5: If need be, you could display just the keys with $Orders.Keys. 

Note 6: Speech marks are only required if the key, or its value, contains a space.

See more about Ordered Hash Tables in PowerShell 3 »

Summary of PowerShell’s Array String

Arrays are designed to store collections of data.  One of my main uses for an array string is to assign $Variables to parameters; while another common use for this construction is in looping.  Some scripters regard the array as a subset of the hash table.

If you like this page then please share it with your friends


See more PowerShell examples for syntax advice

PowerShell Syntax  • PowerShell Array  • PowerShell Array String Conversion  • Plist

Get-Date  • PowerShell Quotes  • PowerShell variables  • RegEx  • PowerShell functions

• PowerShell Tutorials  • Conditional operators  • PowerShell Hashtable   • Windows PowerShell

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.