Let's continue looking at working with arrays in PowerShell.
I'm going to create another array here, $n,
and in $n I'm going to add a bunch of different types of objects.
So I've got some strings, some numbers, and if I look at $n,
alright, so you can see all of those elements in there.
I'm doing this just to show you that you can have
different types of objects in the same array.
If I pipe $n to Get‑Member,
Get‑Member is going to show me all of the object types, so
you can see I have numbers and strings.
So many times we work with arrays and want to test if
values are in or contained in an array, and we can do that with some operators.
So let's do ‑contains.
I want to know if the string FOO is contained in $n, and it is True.
This is not case sensitive.
Does it contain xyz?
No, it does not, so I get a value of False.
You can also test the inverse.
Does $n not contain xyz?
And, of course, now I get a result of True.
Now you can also turn this around and instead of using contains,
you can test if something is in.
So I can say, hey, is xyz in $n?
Again, and the results are going to be True or False.
And there's also reverse ‑notin, is xyz not in $n, and that is True.
Whether you use ‑contains or notcontains or in or notin
kind of depends upon the situation.
Sometimes it makes more sense to be looking if it's contained, and other
times it makes more sense to test if something is in.
Let me give you a practical example.
So I'm going to create a list array, or an array called list I should say,
and it has rdp, searchapp, and spoolsv. And I'm going to run Get‑Process, and
I'm going to filter. And there are other ways you could do this, so I'm going
to do this with the operators here.
So I'm going to filter processes, and I only want processes where the
name property of the process is contained in $list.
So if $list contains the process by name,
then write that result to the pipeline.
And sure enough, I get the rdpclip, SearchApp, and spoolsv in the result.
And let's look at doing something with in.
So I created an array called $allowed, and it has values that I have
pulled from the Get‑SmbShare cmdlet. So here's what I want to do, I'm
going to run Get‑SmbShare, and I'm going to check a computer name, and
I want to know only those shares where the description of the share is
not in $allowed.
In essence,
I want to find all the shares that have been set up that
aren't in this list. And on prospero, you can see that there
are shares for scripts and work.
I could also have written that command using the contains or notcontains,
it's just kind of whatever makes sense to you logically, that's the operator to use.