Let me give you a demonstration of how you might use arrays.
So let's begin once again by creating an empty array, $t.
Now in $t, I'm going to add some elements.
The first thing I'm going to add are three random numbers between 0 and 10,
and I'm using the Get‑Random cmdlet.
The results of Get‑Random will be added to the array.
Here I've created an array, $c, which is a collection of characters.
And again,
I'm going to use Get‑Random and get 3 random characters from that array,
$c, and add it to the array, $t.
Next I'm going to create a string that's just a random word,
and I kind of messed with the cases a little bit,
and you'll see why in a moment.
Strings can be treated as an array, so every character,
and I think we've looked at this before,
can be treated as an array element.
So if I do $w, and then the index number 0 through 2,
this will show me the first three elements in the string
BaNanA that I've treated as an array.
In fact, there is a method on string objects called ToCharArray,
which will turn the entire string into an array of characters.
I'm going to run that ToCharArray method and add the results to the array $t.
So $t now has numbers, it's got characters, and it has a string.
I think you can probably see where this is going to go.
So what's in $t right now, alright, so there are all the elements in $t.
So I'm going to take $t, and you know what,
I want to randomize the elements in that array.
So I'm just going to pipe this to Get‑Random, and I only want 9
elements from that array. That was saved to $rando, so $rando is an
array of nine elements from $t, randomly selected.
Let's join them all together into a new variable, $pass, and
there is a password that you could use.
There's an example of how you could use arrays to do a particular task, and
this is something that you would more likely create a PowerShell function
around, using this code as a starting point.
There is a help topic for joining.
Obviously, you'll want to take a look at that. Next I want to show you a few
other things here with arrays before we move on. Let's go back and review $t,
alright, so we see what that looks like. And we know that we can reference
elements from the array with the index number, so the first element starting at
0 is 3. That works fine when you have created a variable that is an array and it
has elements in it, but what if you try to run a command like this, $var, and I
want index zero.
Maybe $var was something that I created earlier, maybe I forgot to
create it, maybe it was created through some other command that was
supposed to feed results to the variable, but I don't know if there
is anything in that array.
So if I try to reference index 0,
I get an error, because $var has not been created or it's
empty, so there's nothing in that. Again,
because I got an error, that's not necessarily a bad thing, but in
PowerShell 7.0, and this is something that might make more sense to you
when you are scripting, in PowerShell 7.0 we have a new null related
operator that you can use to check if an index is null without having an
error, and this is how it works.
So I'm going to take my variable name,
var, and put it in curly braces, and I've got $, curly braces,
the variable name, and then the question mark, and we saw
this in the other null operators that we looked at earlier,
and then the index number zero.
So this is checking, hey, is index 0 null in $var. Because it's null,
I get null back and I don't get an error message.
You could write code like this.
This is something that we looked at earlier in previous demos.
So I'm going to run that little null indexing operator and then say,
hey, if this is null, then write a warning.
And sure enough,
I get the warning. Again, this is something that you are more
likely to use in PowerShell scripting more than necessarily
interactively in the console.
The last thing I want to tell you, because people will always ask is,
hey,
you showed me about adding things to an array, how do
I remove an element from an array?
Well, you can't. Really what you can do is this.
So let me just create a quick array using the numbers 1 through 10.
If you want to remove an element from an array,
you in essence have to rebuild the entire array. So you have to use some
code to filter out or remove the items that you don't want, and then take
the items that remain and redefine your array.
So I'm going to redefine $a by taking $a, which is going to be 1
through 10, and piping it to where object, and I just want to get
the numbers that are greater or equal to 6.
Those numbers then get written to the PowerShell pipeline,
which is saved to $a. So I've recreated $a, which
is now the numbers 6 through 10.
So if you need to remove some elements from an array, this is how you would do it.