If you want to put a couple of numbers or some strings into an array you can do this:$a1 = 1,2,3,4
$a2= "Bill", "Fred", "John"
You can then use the following to test how many items are in each array:$a1.length
$a2.length
PowerShell is flexible enough to allow text and numbers in the same array, as it sees both as just objects. You can join arrays as follows:$a = $a1 + $a2
$b = $a1, $a2
However, watch out, the first method with a plus sign does indeed take all the elements of both arrays and merge them into one big array, however the comma makes an array with two items in, the two arrays, in other words, an array of arrays, possibly not what you wanted or expected! You can prove this with the length method.
If you want to join an array of strings into a single string then have a look at Working with Strings where this is covered on more detail.
When sorting arrays you might have an array of objects, in which case you might want to sort by a specific property, the following example shows how to do this:$events = $events | Sort-Object -Property TimeCreated -Descending
You can sort in ascending order by leaving -Descending
off, oh and there is -CaseSensitive
if you need that as well as -Unique
if you want duplicates removed.
I have recently noticed that some Cmdlets can return both single objects or arrays of objects but if you don't expect this you can have un-intended consequences! For example Get-ChildItem for FileSystem can return an array or just a single item. If you store the output in a variable then the Length method might return the number of elements in the array or the size of the file, potentially a big difference and confusing. However it is worth noting that ForEach-Object works fine with an array or single object, which is very useful. The sections on "Testing for Arrays" and "Forcing an Array" will also help.
There are times when you need to test if what you have is an array. Here is an example of testing if something is an array:
if ($Files -is [array]) {
Write-Host "We have an array"
} else {
Write-Host "It is not an array"
}
Sometimes you might want to treat an array as if it were a kind of set, this is where "-Contains" comes in handy, you can test if something is in the array.$data = "One", "Two", "Three"
($data -Contains "Two") # output - True
($data -Contains "Five") # output - False
This has worked well for me.
There are times when you only want part of an array, so, remembering that array indexes start at 0, then $data[0..1]
returns the first two items of an array, you can also do $data[1..0]
if you want them in a different order! You can also pick out specific elements rather than a range with $data[1,3,5]
, however you cannot mix ranges and individual elements you need to use one or the other between the square brackets, the "workaround" is to use $data[0..3] + $data[5,7,9]
. Oh and you can assign these into a new variable if needed.
If you are getting information from a Cmdlet or Function you can use "@()" to make sure it is an array, irrespective of how many items are actually returned. For example:@(Get-ChildItem -Path "." -File -Filter "*.txt")
is now guaranteed to be an array, irrespective of how many files are found.
For more information on working with arrays see the following: