1
00:00:01,040 --> 00:00:04,580
Let me give you a demonstration of how you might use arrays.

2
00:00:04,580 --> 00:00:10,340
So let's begin once again by creating an empty array, $t.

3
00:00:10,340 --> 00:00:18,240
Now in $t, I'm going to add some elements.

4
00:00:18,240 --> 00:00:22,830
The first thing I'm going to add are three random numbers between 0 and 10,

5
00:00:22,830 --> 00:00:24,990
and I'm using the Get‑Random cmdlet.

6
00:00:24,990 --> 00:00:29,610
The results of Get‑Random will be added to the array.

7
00:00:29,610 --> 00:00:38,040
Here I've created an array, $c, which is a collection of characters.

8
00:00:38,040 --> 00:00:38,590
And again,

9
00:00:38,590 --> 00:00:45,600
I'm going to use Get‑Random and get 3 random characters from that array,

10
00:00:45,600 --> 00:00:48,120
$c, and add it to the array, $t.

11
00:00:48,120 --> 00:00:52,530
Next I'm going to create a string that's just a random word,

12
00:00:52,530 --> 00:00:54,530
and I kind of messed with the cases a little bit,

13
00:00:54,530 --> 00:00:57,100
and you'll see why in a moment.

14
00:00:57,100 --> 00:01:02,170
Strings can be treated as an array, so every character,

15
00:01:02,170 --> 00:01:03,900
and I think we've looked at this before,

16
00:01:03,900 --> 00:01:06,350
can be treated as an array element.

17
00:01:06,350 --> 00:01:10,780
So if I do $w, and then the index number 0 through 2,

18
00:01:10,780 --> 00:01:15,740
this will show me the first three elements in the string

19
00:01:15,740 --> 00:01:19,540
BaNanA that I've treated as an array.

20
00:01:19,540 --> 00:01:23,950
In fact, there is a method on string objects called ToCharArray,

21
00:01:23,950 --> 00:01:30,440
which will turn the entire string into an array of characters.

22
00:01:30,440 --> 00:01:38,760
I'm going to run that ToCharArray method and add the results to the array $t.

23
00:01:38,760 --> 00:01:43,180
So $t now has numbers, it's got characters, and it has a string.

24
00:01:43,180 --> 00:01:46,140
I think you can probably see where this is going to go.

25
00:01:46,140 --> 00:01:53,140
So what's in $t right now, alright, so there are all the elements in $t.

26
00:01:53,140 --> 00:01:55,260
So I'm going to take $t, and you know what,

27
00:01:55,260 --> 00:01:58,080
I want to randomize the elements in that array.

28
00:01:58,080 --> 00:02:02,550
So I'm just going to pipe this to Get‑Random, and I only want 9

29
00:02:02,550 --> 00:02:09,240
elements from that array. That was saved to $rando, so $rando is an

30
00:02:09,240 --> 00:02:15,640
array of nine elements from $t, randomly selected.

31
00:02:15,640 --> 00:02:21,370
Let's join them all together into a new variable, $pass, and

32
00:02:21,370 --> 00:02:23,640
there is a password that you could use.

33
00:02:23,640 --> 00:02:27,300
There's an example of how you could use arrays to do a particular task, and

34
00:02:27,300 --> 00:02:30,060
this is something that you would more likely create a PowerShell function

35
00:02:30,060 --> 00:02:34,640
around, using this code as a starting point.

36
00:02:34,640 --> 00:02:37,540
There is a help topic for joining.

37
00:02:37,540 --> 00:02:42,180
Obviously, you'll want to take a look at that. Next I want to show you a few

38
00:02:42,180 --> 00:02:47,450
other things here with arrays before we move on. Let's go back and review $t,

39
00:02:47,450 --> 00:02:52,000
alright, so we see what that looks like. And we know that we can reference

40
00:02:52,000 --> 00:02:56,280
elements from the array with the index number, so the first element starting at

41
00:02:56,280 --> 00:03:02,570
0 is 3. That works fine when you have created a variable that is an array and it

42
00:03:02,570 --> 00:03:08,000
has elements in it, but what if you try to run a command like this, $var, and I

43
00:03:08,000 --> 00:03:10,240
want index zero.

44
00:03:10,240 --> 00:03:14,550
Maybe $var was something that I created earlier, maybe I forgot to

45
00:03:14,550 --> 00:03:17,650
create it, maybe it was created through some other command that was

46
00:03:17,650 --> 00:03:21,810
supposed to feed results to the variable, but I don't know if there

47
00:03:21,810 --> 00:03:23,780
is anything in that array.

48
00:03:23,780 --> 00:03:26,840
So if I try to reference index 0,

49
00:03:26,840 --> 00:03:30,220
I get an error, because $var has not been created or it's

50
00:03:30,220 --> 00:03:33,340
empty, so there's nothing in that. Again,

51
00:03:33,340 --> 00:03:37,330
because I got an error, that's not necessarily a bad thing, but in

52
00:03:37,330 --> 00:03:40,990
PowerShell 7.0, and this is something that might make more sense to you

53
00:03:40,990 --> 00:03:45,480
when you are scripting, in PowerShell 7.0 we have a new null related

54
00:03:45,480 --> 00:03:51,650
operator that you can use to check if an index is null without having an

55
00:03:51,650 --> 00:03:55,040
error, and this is how it works.

56
00:03:55,040 --> 00:03:57,260
So I'm going to take my variable name,

57
00:03:57,260 --> 00:04:02,350
var, and put it in curly braces, and I've got $, curly braces,

58
00:04:02,350 --> 00:04:05,040
the variable name, and then the question mark, and we saw

59
00:04:05,040 --> 00:04:07,720
this in the other null operators that we looked at earlier,

60
00:04:07,720 --> 00:04:11,040
and then the index number zero.

61
00:04:11,040 --> 00:04:18,670
So this is checking, hey, is index 0 null in $var. Because it's null,

62
00:04:18,670 --> 00:04:24,840
I get null back and I don't get an error message.

63
00:04:24,840 --> 00:04:29,040
You could write code like this.

64
00:04:29,040 --> 00:04:31,840
This is something that we looked at earlier in previous demos.

65
00:04:31,840 --> 00:04:37,420
So I'm going to run that little null indexing operator and then say,

66
00:04:37,420 --> 00:04:41,940
hey, if this is null, then write a warning.

67
00:04:41,940 --> 00:04:42,700
And sure enough,

68
00:04:42,700 --> 00:04:46,260
I get the warning. Again, this is something that you are more

69
00:04:46,260 --> 00:04:49,820
likely to use in PowerShell scripting more than necessarily

70
00:04:49,820 --> 00:04:51,840
interactively in the console.

71
00:04:51,840 --> 00:04:54,170
The last thing I want to tell you, because people will always ask is,

72
00:04:54,170 --> 00:04:54,730
hey,

73
00:04:54,730 --> 00:04:57,430
you showed me about adding things to an array, how do

74
00:04:57,430 --> 00:04:59,370
I remove an element from an array?

75
00:04:59,370 --> 00:05:02,820
Well, you can't. Really what you can do is this.

76
00:05:02,820 --> 00:05:08,740
So let me just create a quick array using the numbers 1 through 10.

77
00:05:08,740 --> 00:05:12,340
If you want to remove an element from an array,

78
00:05:12,340 --> 00:05:16,810
you in essence have to rebuild the entire array. So you have to use some

79
00:05:16,810 --> 00:05:21,620
code to filter out or remove the items that you don't want, and then take

80
00:05:21,620 --> 00:05:25,840
the items that remain and redefine your array.

81
00:05:25,840 --> 00:05:30,460
So I'm going to redefine $a by taking $a, which is going to be 1

82
00:05:30,460 --> 00:05:34,120
through 10, and piping it to where object, and I just want to get

83
00:05:34,120 --> 00:05:37,540
the numbers that are greater or equal to 6.

84
00:05:37,540 --> 00:05:40,870
Those numbers then get written to the PowerShell pipeline,

85
00:05:40,870 --> 00:05:45,650
which is saved to $a. So I've recreated $a, which

86
00:05:45,650 --> 00:05:48,640
is now the numbers 6 through 10.

87
00:05:48,640 --> 00:05:54,000
So if you need to remove some elements from an array, this is how you would do it.

