Oh snap! Stuff are getting interesting. First What is an Array. In powershell or most other languages arrays basically it is a data structure that can serves as a collection of multiple items, for example if you want to organize multiple books. You will use a Bookcase right!? or some other thing. But a bookcase, it is kind of made up for books so lets just say that we are going to create an array call bookcase that it is empty.
The way we will go about it is as follow so look below.
$bookcase = @()
that means that there is an empty array of books called bookcase. right now it is empty. if we want to add data or lets just say that if we want to add books into the bookcase it would look as follows;
$bookcase = @('lord of the rings' , 'Head First Java', 'Head first PHP')
I can also say that the array called bookcase has three books and those three books are lord of the rings, Head first java and Head first php.
Please notice the syntax it is always like that when we want to put strings inside the array. Strings are basically pieces of text enclosed with single quotes or It is a datatype that denotes the sequence of characters, either as a literal constant or some kind of variable. That’s technical ha!
Basically everything you put within quotes will be a string to keep it simple. There are lots of stuff that we can do with Strings but we will get to that later.
The is an older syntax to create arrays on Powershell but I’ll let you do that yourself.
To access individual items, we use the brackets []
with an offset value starting at 0. This is how we get the first book or item in our bookcase array:
$bookcase[0]
If we want to display that first book or the book lord of the rings, you can use the Cmdlet Write-Host.
Go ahead and type Get-help “write-host’ in powershell to see what you get. Get used to type stuff in powershell. the reason why is because the more you do it the better that you will get.
How would you get the second element of the array to display?
How would you display all elements? We can go about this in multiple ways. Sometimes it is good to write scripts using VSCode. I didn’t cover this but if you are here you probably know that all scripts that finish with .ps1 are powershell scripts. if the script finish with .php that means it is a php script. if it finish with .py it is a python script. Let’s say that you want to make a script that display elements of the array using a for loop how would you go about that? It would go something like this:
for ($i = 0; $i -lt $bookcase.Count; $i++) {
Write-Host $bookcase[$i]
}
You can also use a foreach loop example is below:
foreach ( $node in $bookcase )
{
"Item: [$node]"
}
You can display arrays also using the foreach method.
$bookcase.ForEach({"item [$PSItem]"})
Try this is in the Powershell console and change a few things such as changing letters, basically you want to see how you can have the console tell you that there is an error. for example take a bracket off, learn how to read the errors. You can also drop the parethesis in the for each method and it would work too.
This is all for now: That was a quick and dirty read about Arrays;
Task1: For lab Using VS code write a .ps1 script called showMeArrays
Task2: Inside the script Create an empty array called Battalions
Task3: Inside the script Create an array called companies and put inside the array, Alpha, Bravo, Charlie, Delta
Task4: Create an Array of integers called plattons and inside that array put the platton numbers 1,2,3,4
Task5: Iterate through all arrays that you created once.
Task6: Create two more arrays of your choice, NOT BY COPYING AND PASTING and talk teach someone about it even if noone is reading. The perfect platform to do this would be linkedin, Facebook or Twitter.
Task7: Research about Arrays and Methods that are used around specially Where-Object filtering, create a document for yourself with all the links that you find and post the links in the comments.
You will be surprised everything that you learn when it comes to arrays. Learn the way for loop and foreach loop works because down the road you will most likely use it.
Below are some of my references: