Mastering PowerShell Dynamic Arrays
1K views
May 7, 2024
Learn how you can make your PowerShell life easier using dynamic arrays! š Get the Code Here! š https://github.com/JeffBrownTech/jeffbrowntech_youtube/tree/main/2024-05-06-PowerShellDynamicArrays ā Read: Mastering PowerShell Dynamic Arrays ā https://jeffbrown.tech/powershell-dynamic-arrays/ ā° Chapters ā° 00:00 Introduction 00:18 Defining Arrays 01:06 Access Array Elements 02:01 Add and Remove Array Elements 05:26 Create Dynamic Arrays 08:41 Convert Fixed to Dynamic Arrays 12:30 Summary Social Links š LinkedIn: https://www.linkedin.com/jeffbrowntech š X: https://www.twitter.com/jeffwbrown
View Video Transcript
0:00
What's up everyone? Today we're going to talk about PowerShell arrays. We'll look at fixed arrays versus dynamic arrays, how you can work with both, convert between the two, and access elements within them, and basically all about PowerShell arrays that you need to know to get started with them. So let's go ahead and get started. There are multiple ways to define arrays. Here I have two different ways on lines two and three. You can just do a list of string, numbers, maybe objects, just separate them
0:30
by commas, line 3, they're enclosed in parentheses and an at sign at the beginning
0:35
I usually like line number three format just because visually the at sign and the parentheses
0:41
indicate to me that it is an array when I'm looking at a script. But if we take a look at both of these, we'll run this one
0:51
And if we look at the Git type, it's a system array
0:55
We do the same thing with the other one. It's also a system array
1:01
There should be both exactly the same thing there in defining them
1:06
So I have an array here called animals. It's got a list or an array of elements, different animals in them
1:14
If you don't know about arrays, you can access the items within the array using indexes
1:20
And those indexes start at zero. So if we were to look at index zero, it would be
1:25
be cat. In this case, if we're looking at index 1, this should return dog. So that's how we can
1:31
access that element within the array. You can access a range of elements like on line 7 using
1:38
2. So that's going to be bird dot dot all the way to item number 4. So we look at that. We've
1:46
got bird, horse, and rabbit. And my personal favorite, you can always use negative 1 to access
1:51
the last element in the array if you don't know how long that array is
1:55
So if I do negative 1, we have Pig as the last one in the list here
2:01
Let's say you have an array like this, but you need to add items to it
2:06
Maybe you're looping through a script and you need to add something before you output it at the end
2:11
You know, you're collecting some type of items. You might immediately look if you're tabbing through the properties on the array there and you see something called add
2:21
So you might be like, oh, let's use add. I want to add to this array. So we'll go ahead and select this and run it
2:27
And it's going to come back with an error, just saying the collection was of a fixed size
2:33
And one way you can also verify this is there is a is fixed sized property or attribute on the array that will return if it is of fixed size or not
2:44
We can see that is true. What this is saying is this array is of fixed size
2:49
You can't add to it using this add method. You can't make it any bigger
2:54
But that really doesn't solve what you may be trying to do in your script of you would need to add to an array as you're working through it
3:01
With a fixed size array, there is a workaround that you can do and you can use the plus equal operator
3:07
If you don know what this operator is you basically saying animals equals animals plus fox on line 20 You just adding what is on the right side of the plus equal sign to what on the left side of it In this case it our existing array
3:24
Now, I do have a comment here in a note. This method is perfectly fine if you're using it in small batches or increments
3:32
but if you have a large script where you're doing this lots of times and the array is getting bigger each time
3:38
it can lead to performance issues because what it does is it makes a copy of that existing array
3:44
and then adds the other element to it. So throughout this whole script
3:48
you might be taking up more memory processing power of always copying this array
3:53
in addition to the one that's currently existing. If you're doing it in small increments
3:58
I don't see any problem with that. But again, if you have a large script that's doing something thousands, millions of times
4:04
could be a performance issue. Let's take line 20. Let's add this other animal here to our array
4:10
You see, we did not get any errors back there. And if we take a look at our array now
4:16
we can see that Fox is now at the end of the array. If you want to remove an element
4:24
you can't do the minus. You can't subtract from the array like this
4:28
But there is an option with the not equals operator here. So you can say animals, not equal to bird
4:35
it's going to output all the animals that don't equal bird, effectively removing it from the array
4:42
If we run that, we can see that output there. And I believe it does save it
4:48
Actually, no, it doesn't. So now it just says bird is back in there, but for the time being
4:52
it removed it from our output. But I wonder if we were to do something like this
5:01
and then rerun animals, we can see bird is now not a part of it
5:06
So as we had to do was save it to our array. there to remove that element from it
5:13
This is fixed size arrays. Initially, when you maybe try to use those methods
5:17
it comes back and said you can't do it, but there are some workaround you can have here
5:21
As we mentioned, could have some performance issues if you're doing it lots of times
5:26
However, there is another option we can use, and we can use .net classes to create dynamic arrays
5:33
That's the great thing about Parishel. It's built on dotnet, and you can access other things
5:38
within the dotnet framework, to enhance your scripts outside of maybe some of the things that are just built in
5:43
Let's first take a look at an array list example. I have my new variable here, Animals Array List
5:51
We're going to access the system collection array list class, and we're specifying we're creating a new array list
5:59
Now we can use those add methods where we say dot add
6:03
and we start adding elements into our array. Let's go ahead and add some elements to it, so I'm going to highlight this and execute it
6:14
And then now let's take a look at our animal array list, and we have the three items that we add to it
6:22
Now you notice in my output as we are adding each element into our array list it starts outputting the index that you inserting into So we see 0 1 and 2 If this script is running in some automated task where you not going to see the output this is not that big of a deal
6:41
You won't see it, nobody will see it. But if you're running a script or a function that is outputting something to the screen
6:47
this could get really annoying as it's adding in there. And maybe you're doing this hundreds of times
6:52
You're seeing all these numbers fly across the screen. We do have options to suppress this index output
6:58
Let's take a look at those. Go ahead and define a new array here again
7:04
One thing you can do, we'll look at line 38 and 39, you can add void to the beginning of when you tried to add it in there
7:11
So it will void out any output. So let's go ahead and highlight these two
7:16
As you can see, there are no index outputs. Another thing I believe you can do is you can pipe it out to OutNole
7:23
That's a PowerShell commandlet. You can take this. That also successfully suppress the index output
7:29
and if we go back and rerun Animals Array, we have our three items in it
7:36
I have seen some debate and like stack overflow about which one of these is better. Do you put void
7:41
at the beginning? Do you pipe it out to out null? I believe
7:45
the consensus, or at least what people were testing, was adding void at the beginning is a little
7:49
bit faster because on line 40 if you have to pipe it out to out
7:54
null, that does maybe add just a little bit of processing power
7:58
But again, if you're doing it On small scales, either one is probably fine
8:03
Line 40 is maybe considered more PowerShell-like because you're piping it out to a PowerShell commandlet to Out-Nole
8:10
But again, if you're doing this lots of times, maybe try both ways in your script, see which one is faster for you
8:16
With our fixed arrays earlier, we did not have an easy way to remove or subtract elements in our array
8:22
But now that we have an array list, we can just remove elements by name
8:27
So right now we know we have Cat, Dog, and Bird. I can do dot remove, item name
8:34
And if we just rerun our variable, we can see now it just has the two elements in it
8:39
cat and bird. Let's say you've done some type of import or you have an existing array that is a fixed size
8:46
Luckily, you can convert it to an array list or a dynamic list
8:50
and then be able to work with it after that. On line 46 here, I have an example again
8:56
of just declaring a fixed array with certain elements. elements in it
9:01
And then what I can do is on line 47, I have array list, and before array list in my definition
9:07
I'm specifying the data type that I want it to be. So just like we saw earlier, system
9:13
Dot collections. Array list. We're going to set that equal to our fixed array
9:19
And then we should be able to add to it. Perfect. And we saw the index output there, no big deal
9:25
And then again, if we do array list. Digit type. It's an array list, system object, and then we can look at is fixed size, and that is false
9:39
Just showing you how you can take an existing fixed array and convert it to a dynamic array list
9:46
Something that different about generic lists is you have to define the data type used in the collection Unlike what we did earlier I believe we could sit here and add any type of data type In this example we had just strings
10:00
that we've been adding in here, but just actually take a look here. Let's do array list.ad
10:06
And if I were to add a number, I don't know if this will work
10:12
That did work. Let's go back and look at our values and our array list
10:17
We have all the strings and then also numbers. So our array list type, we could have multiple different types of data types in there
10:24
We have strings, integers, probably objects, whatever else you want into it
10:29
Generic list, you have to define the data type that is going to be used inside of your generic list here
10:37
I have two examples of that, line 54, at my string list, system collections.generic
10:42
And then in the square brackets, we're saying this is going to be a generic list of strings
10:47
and then our new constructor. Same thing with in-list. We're just saying we're putting integers in here
10:54
So let me go ahead and highlight these and define each of them. Then we'll come down here and same thing, we can add and remove to them
11:01
One thing that's different with our generic list, if we add to them, notice there's no index output, so you don't have to worry about that
11:08
So we can add string one, string two. However, if we do remove from them, it does have some
11:17
that it outputs. Let me just highlight this here. Change this to string 1. We'll highlight the command
11:26
So it does output true or false, meaning if it successfully remove that element. So that's
11:30
why I have void here on line 61. So kind of the opposite. With our array list, if you add
11:36
something, you have to null it out. With generic list, if you remove something, you have to null it out
11:42
And then same thing here with my end list. I'm going to add some integers to it. And then void
11:47
out my removals here. So if we do my int list, just has three in it because we removed one
11:56
All good there. And just like when we had with array list, you can convert an existing fixed array to a generic list
12:05
Here we have fixed array of just our animals again. And same thing on line 68, you're going to put in your data type before your variable
12:15
name and set it equal to your fixed array that you're trying to convert
12:20
Perfect. And that comes through. And now if we do generic list, that comes through
12:27
Is fixed size is false. Well, that does it for talking about dynamic arrays and how you can take a fixed array
12:36
and make a dynamic in order to add or remove elements to it a little bit easier and maybe
12:40
a little bit improvement in performance besides using the plus equal operator
12:45
converting them, all that fun stuff. That does it for this video
12:50
Thanks for watching and we'll see you next time
#Programming