So let's go back into the PowerShell console.
We'll create a custom data object.
We'll first also retrieve data from the custom object by
loading and reusing that custom object.
We'll then test those objects using that ‑IS operator.
Now, PowerShell provides us the ability for using custom data,
different types of data that we can store or retrieve.
Now, the easiest example here would be to create a new variable,
and we can use the @ and the brackets here to actually create an array,
and that's effectively an empty array.
So if I say variable, and just do this,
there'll be no values that are associated to that.
Now, if I wanted to change that and actually utilize values,
then we can simply use text values here,
so Jan, Feb, Mar, Apr, May, and Jun.
Okay, now we can say $variable, and we get our values back,
so it's an array of those objects.
Now, that's a standard array with text values.
Now, we've also created arrays already,
where we've actually created an array by not using the definition of an array,
but by adding the values in like this, which works perfectly fine,
so that's one way of doing it too.
You don't have to declare by using @ and brackets every
time you want to create an array.
However, what that does give us is,
if we just go back here and create the empty array,
is that when we say variable.Add,
we get this kind of function method that's available to us to add entries to it,
whereas if we've created the variable without that,
we don't have that flexibility,
so there is a reason for creating them that way as well.
So let's just go back here and populate that with some values,
okay, so now we have that.
Now, if we want to access the data in here,
you can see that we could just type the variable and it would output the values.
But, of course, as with all sets of data,
we do have the ability to utilize an offset.
So, for example, if I use 0,
that's the first piece that was chopped out in the array,
I get February, sorry, January.
If I do 2, then I'll get March, because, remember, it starts at 0, not at 1.
It also means that I can do this, too.
I can then use different values to be able to retrieve those items from there.
So 0, which is January, 1 is February, 4 would be May,
for example, so I get those values back.
We also have the ability to use, so 0..3,
so using that range operator that we used previously for adding data,
we can use that for retrieving values as well,
so really simple kind of ways of accessing the data.
Now, what's nice is once the data is in a variable,
then of course we can retrieve the data either that way or
we can retrieve it by piping the values.
Now a pipe option allows us to take the values or something
from a command and pass it into the next one.
So I'm going to use something that we've already talked about,
which is called ForEach‑Object.
So I'm going to say take everything that's in the variable,
and for every single one of those, in my script block I want it to do something.
So I'm going to go in and say Write‑Host.
So I'm going to write a value and say The month is,
and then I can say $, I missed my letters there,
PSItem, and close that off.
Now, PSItem is a value that's returned from the ForEach‑Object.
And, of course, when I enter that, that's what it does,
the PSItem is an arbitrary one that's there,
that when you use ForEach‑Object, that's what comes back.
So now I can say, hey, here's my variable of data,
for each item in there, iterate it, and then do something,
and in this instance I'm writing out values to the screen.
So that's one way of doing that.
The second way of doing that is to use the trusty old
foreach statement that we used before, where I can say $item in $variable,
and then do the same thing as we did before,
where we kind of output in that inside the script block here.
So Write‑Host, and I'm just going to say $item, like so.
And, of course, you get January all the way to June, so nice and easy.
So we can use all of the different looping mechanisms that we
used previously to actually get the data back,
which is really the most common thing that you would do with data that you get.
You want to iterate through it, and you want to do something with it.
Now, an array is great, but really it's just a single value,
well, I say single values, but it's multiple values,
but there's just a single value for each one.
So we can also create data, so I'm going to call it variableh,
and then I'm going to say @ and I'm going to use squirrely brackets this time.
Now notice that's not an array this time, that's a hash table.
Now a hash table is a series of keys and values, so let me give you an idea.
So I've got my variableh even, and from here I can say @,
squirrely brackets instead of the regular,
and then I'm going to say Month=5, Name=May,
Season=Spring.
So let's put some of these in quotes,
because they're string values that we need to return back,
so let's do this, and let's do that.
Okay, now notice what happened here.
It says that I have some syntax errors, and that's great,
because what that means is that notice how we would
normally comma separate things, we actually,
when we're doing key‑value pairs, we have to use the semicolon instead.
Now, what that does for us, say if I do variableh and Enter it,
you can see I then get a name and a value,
so Month is 5, Name is May, Season is Spring.
Now, what does that do for us?
Well, let's look at this.
So what we can do is I can say variableh.Count,
and you see I've got all these different things that are there.
I could say Values, and this will go and get me just the values.
Or, I could say get me the Keys, and it gets me the keys.
So you have the ability to store values with a key
that you can then reference later on.
I can also change the order around as well, if I needed to.
Let's think a little bit kind of wider here.
I'm just going to copy something and paste it in,
just because it's easier for the text that I'm using.
So you can see I'm creating a new variable as a hash table and I
have four countries in here with their population,
so I'm going to press Enter here.
Now, what that means is I can go to variable,
and I now get this matrix of a table that basically
says this country has this value, etc.
So how can we retrieve some of this item?
Well, the first option that we've got is like we just did,
we can say variable.Keys, and I get the values of that,
but I don't get the value associated to the keys,
so we need some way of kind of iterating through them.
Now, the simplest way of doing this is to do a foreach statement,
and I can say $key, so that's an empty value,
in, and then I'm going to say variable.Keys.
So what I'm doing is I'm going to iterate through the sets of keys,
and I need to be able to retrieve value.
So I'm now going to create a new variable called output,
and I'm going to use some kind of string manipulation here,
has a population of, and another one of these,
so 1.
So if you've never seen this before,
this is a standard constructing code where you're formatting
a string value and you use the little squirrely brackets
with a number that represents 0, 1, 2, 3,
4, etc.
I'm then going to go in and say for the first value I
want that to be what's called $_, which is the key,
and then the next one is $variable, brackets,
remember, is used for the offset,
but I'm just going to use the identifier piece there and press Enter.
Once I've done that one, then I'm going to say Write‑Output $,
oh, I don't want all of that, I just want the smaller variable.
Let me just change this.
Let me just do Write‑Host instead.
I was trying to write out too much.
(Writing) Output, there we go, and close that out, and Enter.
Now, what happened here?
You can see that I've got loads and loads and loads
of errors all over the place.
It doesn't like what's taken place here.
So let's kind of break this down what it's trying to do.
So what you can see here is we've got a foreach statement,
which is going to go and loop through the values,
and you can see that we're trying to use this $_.
Now hold on a minute, what is a $_?
Well, a $_ is an object that returns back,
yes, wait a minute, I can hear you thinking,
it's like the PSItem, which means it can't be returned in a foreach statement.
So in a foreach statement, we have to use the named objects that come back.
So let's go back through the code, so here's my output,
and we're going to change these values,
because the $_ is only available for a foreach.
So what this is going to be is the key.
Where does the key come from?
From the entry in the beginning of the for loop.
If I then go here and say $key, like so,
I can then say Write‑Host, and I'll just say,
sometimes it's not best to use words that match everything else,
and close that out, and then Enter.
And sure enough, it now works.
It now says Spain has a population of something,
blah, blah, blah, blah, all the way down to India has a population of,
and something.
So It's really important to understand that when you see examples
of PowerShell code that's available to you,
just make sure that you're not mixing and matching.
So aForEach‑Object loop would look very similar,
so let's just kind of show you what that would look like.
I'm not going to run all of it, but show you.
You would do the variable, you would do a ForEach‑Object statement,
like so, and then for this row you would do that same thing that we did,
so 0, some text, 1, close that out.
Then you get access to the _$, and then you can say variable,
in the brackets $_.
Once we have that, Write‑Host $output, and do that, and Enter.
And then, of course, depending if you've got all the variables right,
it'll then write out the variable values that you're expecting.
You noticed why it's come back with a hash table?
Extra points if you can get this right,
but I'm going to give you the answer anyway,
is because the variable that we actually want to iterate is variable.Keys,
not the actual variable itself.
So if we go back here and say Write‑Host and do that,
you'll get all the values now.
So it's really important to understand the definition of
the object that you're trying to get to.
Okay, so let's just clear that for a second.
Now,
one of the last things that we can look at here is really around
creating what's referred to as a custom object.
Now, PowerShell obviously has the PSCustomObject,
and most things get returned as a PSObject.
So I'm just going to copy some code here, and I'm just going to paste over here.
Now you can see the code looks almost identical to the last one,
except the difference is right here we've declared
that object as a PSCustomObject, so we'll just enter that,
so now we have a value.
So if I go back and say variable, notice it lays it out a little bit differently,
but you still get the same values.
You get China, India, America, Spain, and the different ones.
Now,
the nice thing here about using a CustomObject is that we
are able to add things to the object.
Now, I'm just going to copy a value here.
So you can see the value.
I've chosen that same variable, and this time I've piped it into Add‑Member,
because I wish to add a member.
And PSCustomObject has a NoteProperty, so I'm going to add this one to it.
So if I go back to variable now, you'll see that Russia has now been added.
If I go and just copy and paste another one here,
there we go, we can see Norway.
I can enter that, let me just clear the screen, and then do variable.
You'll see we've now added to the original variable
itself because it's a CustomObject.
So that's the beauty of this.
Now, what does that give us over everything else?
Well, let's have a look at how we can retrieve values from this.
Now notice I can just do variable.China and it gives me the value.
I can do variable.Norway and I get those values back too.
So it means I'm able to return the value from the CustomObject really simply.
So, it's a nice way to store values, I suppose,
almost in like an indexable way, where you're able to return the values.
If I wanted to just get everything, then of course I could just do that,
but of course if I tab through, I get the values,
Spain, I get equals, I get all of the regular things,
but all of those become properties on the object,
so it's a nice way of being able to manage information that we need to populate and add in and reuse later.