Next, let's turn our attention to hashtables.
Now, you may sometimes see hashtables written as two words.
There is an actual .NET type that is hashtable as a single word,
so I'm just going to be using the single word,
hashtable.
A hashtable, again, is another type of data structure, key value pair.
Sometimes this is referred to as like a phone book where a phone book
has the name of someone and their phone number.
If you're a VB script kind of person,
we had something called a dictionary object,
which is the same idea as a hashtable.
I have a key and some corresponding value.
In the hashtable, the keys are unique,
the values can be whatever they need to be, and here is how we work with them.
In PowerShell, you'll often use hashtables to store data.
Hashtables are very flexible, they can also be used to
create custom properties and values.
You can create something that doesn't exist and you
can use a hashtable to do that.
Oftentimes, we can use hashtables as a lookup, remember,
it's a name value pair,
so you can create a hashtable of keys and then lookup some corresponding value.
This is something that might be more useful when you get to
partial scripting. And we can also use PowerShell hashtables
to simplify running command, this is splatting, and again,
I'll get to this in a little bit.
So let's create a hashtable.
Hashtables are very simple to create. The hashtable syntax is
the @ symbol and then a set of curly braces.
Inside the curly braces,
we define our key value pair separated by semicolons if
it's all on the same line, otherwise,
if you just hit Enter after the key value pair,
PowerShell knows okay, whatever is next is another set.
This hashtable $h has two key value pairs, one called name,
which has a value of Jeff, and the other key pair has a key
of Count and the value of 3.
So again, we have the @ symbol Key = Value. Technically yes,
you could have a hashtable of just a single key value
pair, but more than likely, you will be creating multiple
entries inside your hashtable.
They're separated with a semicolon if you're on the same line.
Otherwise, if you just hit Enter, then PowerShell, as I said,
knows what you are doing.
So the hashtable I created is saved to $h. If I type $h, I can see
the hashtable, and in the output, you can see name and value. Name
is the key and then value obviously is the value. So I've got name
was Jeff and count of 3.
Where this gets fun is that you can reference a hashtable
and get a key value just by referencing using the
object.notation like we've done earlier.
So I can do $h.name and that tells PowerShell hey,
show me the name key, show me the value that you have associated with that,
and so I get a value of Jeff written to the pipeline.
So even though I've created a hashtable, it's still just another
type of object and I can use the object.notation in order to access different elements in that hashtable.