Okay, so now that we've worked with standard data values,
variables, and string values, let's talk about custom object data.
The advantage here is that we want to be able to create something
custom that matches the type of data that we're using.
Now, a custom data object could be an array.
It could be a hash table.
It could also be a custom object.
So, what are arrays?
Well, an array is a list or collection of values or objects,
and we've seen some of these already where they're a comma‑separated list.
Most important thing here, arrays only contain values, no properties.
So there's no something on an item dot something as a property.
That doesn't exist.
Now to create an array,
empty arrays are created by using the @ and then parentheses.
You can have comma‑separated lists as arrays,
and then, of course, the Write‑Output command can also create string arrays.
So if you're just using string arrays of data,
there's already a command that can help us do that.
So to create an array, an empty array, it's going to be $variable = @().
That will create an array, and then we can add values into that array.
We could create an array with existing values.
So I'm using January, February, March, April, May, June.
They are in text values and comma‑separated,
so that populates my variable array automatically.
I could create an array with text values, but not use the @ and parentheses.
That would create a standard string array.
And then I can also use the Write‑Output to do the same thing.
So we're not using the @(),
we're just using Write‑Output and these values with separations,
and that will create an array.
Now, of course, once we have the arrays,
then, of course, we need to look at how we retrieve array items.
So I'm using $variable = @() and then January,
February, March, April, May, June.
If I wish to retrieve a value,
the most standard and easiest way is to use what's called Offset.
Offset will be the unique ID or placement of that value.
So if I do $variable[0], that will get me January.
If I do $variable[2], that will give me March.
It always begins at 0.
I can access an array item using multiple offsets as well,
so I could say get me variable offset 0,
January, 1, February, 4, April.
That would actually get April or May.
That would give me those three values.
I can access the array items using a range operator as an offset.
So just like we did previously where we populated a
variable with 1 to 10 by using 1..10,
I could do the same here and say go and get me the
range operator offset of 2 to 5,
which would obviously take me over to not January,
not February, but start at March and go upwards.
Now we also often want to iterate array items.
So not only are we just taking odd things out and getting single values,
we may want to iterate.
So once again, we create the variable.
And you can see we're using a ForEach‑Object.
So this means I'm going into the variable.
I'm using a pipe command,
which we've talked about in the past where we're piping whatever's in the
variable to the next piece and saying ForEach‑Object,
and then I'm just literally writing out the value,
so the month is whatever that would be.
And you can see we're using something called $PSItem.
That is a standard PowerShell property or variable that's created by
PowerShell for the output of a ForEach‑Object loop.
We can do this differently by using a foreach statement that we've used before,
so we can say foreach item in that variable and then
go ahead and write the values out.
I could also use the ForEach method.
So knowing that the variable contains the values,
that allows me to say ForEach and then write those values out.
And then, of course,
our other option is to use a for loop to go through the values.
Now you can see the code difference here, single lines to multiple lines.
So, for example,
that last one is foreach item equals 0 if the item
is less than the variable count.
So if there's seven things in there, it will iterate seven times and then stop.
Now, another version of arrays, I suppose, is a hash table.
So the hash table is a data structure, very much like an array,
except you can store every value, an object,
using a key.
So hash tables only contain values, not properties.
They are basically a key value store.
So when we look at the hash tables,
they're also known as a dictionary or what's called an associative array,
the data structure that stores one or more key value pairs.
So there will be a unique key that represents the value.
Keys and values in hash tables are defined as .NET objects.
And then, of course, it's an efficient way of finding and retrieving data.
Now to create a hash table, we once again start with the @ as before.
You wrap the hash table in braces, so you can see we've got our combination here.
You can use an = to separate each key from its value,
and then you use a semicolon or a line break to separate the key value pairs.
So let's look at how we create the hash table.
Well, first off, I'm going to create my empty hash table.
Now don't get confused here.
When we created the array, we used @, and then we used parentheses.
A hash table is an @, and then what I refer to as the squirrelly brackets.
What we can then do is create what is the key and values.
So you can see here I've got Month = 5; Name = May; Season = Spring.
I could go a little bit further here, and I could create an ordered hash table,
which would order those values by the keys and values.
Now to display values from the hash tables,
we first store the hash table in a variable,
so we basically say $variable name, and then press Enter.
We can then use the dot notation to display all of the
keys or the values inside that variable.
So, for example, $variable.keys would return the key values,
whereas $variable.values would return the values.
Each key name is also a property.
So, for example, $variable.Month, which would be one of the values,
would return a value.
I can also do that by using $variable and then square
brackets and in text type in the value or the property name
that we wish to retrieve the value for.
So to iterate a hash table,
let's say we create a country population hash table here,
so I've got $variable equals a hash table,
and in the hash table is four countries with their population.
To iterate all the keys and values, I could use a ForEach‑Object loop.
So I'm saying $variable.keys.
So for each key, loop inside of it, and then the output of that will be,
you can see I'm using a different syntax here where in the string
values I have a 0 and a 1 in squirrelly brackets.
That means I'm going to fill those values with something else,
and you can see the ‑f,
which basically says format the output of this in this way.
So I want a value, something, which would be the name of the country,
has a population of something, which is the value.
So what you can see here is because we're using a ForEach‑Object
and we've already told it to iterate the keys,
I don't need to call the key value because in the ForEach‑Object,
it's iterating them anyway.
So I can do $_,
which will represent the names of the countries or the keys from the hash table.
And then you'll see in the variable option,
I've told it to go ahead and get the variable,
and then all goes together in a Write‑Output.
Now we can iterate all the keys doing just a standard for
loop like a standard regular foreach loop.
So I can say foreach key in the keys, output,
format the same thing, and then output the value.
So iterating a hash table is very much like Iterating an array.
Now we have another type of object here called the PSCustomObject.
So PSCustomObjects are a really simple and easy way to create structured data.
A PSCustomObject is comprised of both properties and values.
So a PSCustomObject is like a hash table,
except it's strongly typed as what's referred to as a PSCustomObject.
The properties are ordered by default.
And then, of course,
the PSCustomObject contains what's referred to as a NoteProperty.
So this is the same as common PowerShell objects that are part of
the core PowerShell construct in the language.
Now to create a custom PSObject,
we're going to use syntax that's very similar to what we've chosen before.
So we're going to say $variable =, and then we're going to strongly type,
so we're going to say [PSCustomObject]@,
and then the squirrelly brackets,
which will create us effectively the hash table,
but it's a PSCustomObject.
We can also create an empty one in a different way by using what's
called New‑Object with a ‑TypeName of PSObject,
so two ways of doing this.
That's actually quite a common way of doing it as well.
Now if I want to create and populate the custom object
using the countries that we used before,
you can see that I just adjust the variable and say PSCustomObject,
and then it's still the same kind of text values.
Now if I want to add items to the custom object,
now I can because I have this ability to add to the PSCustomObject.
So you can see here, I'm saying choose the variable,
pipe it out, and say add a member to the PSCustomObject.
And the ‑MemberType is a NoteProperty, and then give it a name and a value.
And so I'm now adding Russia and Norway into the list of countries.
Now what about if we want to retrieve those values?
Well, we have our standard PSCustomObject. And this time,
I could return all the PSCustomObject key value pairs by
just typing out the variable itself.
If I wanted to get the country, I now have the ability to do so.
I can say $variable.Spain.
That's it, and it would return the value.
We also have the ability to test the PSCustomObjects.
So the type comparison, which would be kind of IS and ISNOT,
are used to determine if an object has a specific type.
So I could say is it this type, is it not this type?
So what does that look like in PowerShell?
Well, $variable1 = to number 10, so it's not a strongly‑typed variable.
$variable2 is equal to 10, but it's a string version of that one.
And $variable1 I'm trying to convert to an integer,
so I'm saying $variable1 is an integer.
I could also say $variable1 ‑is $variable2.GetType().
So I'm trying to match it now and say are they the same?
I've also got, well, $variable1, is it not an integer?
So I can do that too.
And then I can do ‑isnot $variable2.GetType.
So that ‑is will allow me to check.
Now, what does that look like,
and what error messages do we get when we come back? We'll have a look at that in a moment as well.