Obviously,
there is a lot more that we can do and will want to do with hashtables.
So once again,
let's go back into PowerShell and let me demonstrate
how you can work with hashtables.
To create a hashtable, you can either create an empty hashtable like this,
so $ht = @ symbol and a set of curly braces,
or you can create a hashtable and assign values as you create it.
This hashtable has three key value pairs.
The first one is name with the value of jeff,
count, which has a value of 3, and then Sample Entry,
which has a value of This is something.
Notice that sample entry has double quotes around the name of the
key and that's because there is a space in that.
Typically, I would avoid using spaces in my keys,
it just simplifies everything in PowerShell,
but if you need to have it, that's how you would do it.
Because I'm doing this all on one line,
I use a semicolon to separate the key value pairs.
The hashtable is its own special type of object.
You can see there it's a System.Collection.Hashtable,
and if you want to view the hashtable,
just obviously look at the variable and there we go.
So there are my three keys which show up as name and
then the value that goes with each one.
If I want to, I can add an element to the hashtable,
so I'm going to add a key called version with a value of 7.
Now you can see there is my new entry.
If I want to, I can also change values in the hashtable.
So I'm going to take that count property and use the ++ operator which
will take the value of count and increment it by 1,
and sure enough, it is now 4, or I can also assign a new value such as 10.
You can reference elements by their key name.
So if I want to get the name entry from the hashtable,
I can do $ht.Name and I get Jeff as the result.
There are a couple other ways that you can reference items in a hashtable.
You can use this syntax where you put the key name in square brackets,
it gives you then the value,
and I think this approach is easier if you have a
key that has a space in the name.
Another option that you can use is the item method and all you
have to do is specify the name of the key,
in this case, name and I get the result of Jeff.
With hashtables,
you can list all of the keys and you can also list all of the values.
If you need to remove an element from an array,
there is a remove method and just specify the key.
There is nothing really that shows you that it's
gone until you look at it again.
It is possible to completely remove all of the entries
in the array using the clear method.
And now if I do $ht, I can see that there is nothing in it.
The variable still exists, but is an empty hashtable.
You can use hashtables to really store just about anything that you want.
So let's build another hashtable.
I'll create my entry name and now I'll do the count again for 3.
Notice that because I'm not doing this all on the same line,
I don't have to put in the semicolon.
As soon as I hit Enter,
PowerShell knows the next thing that I type is
going to be another key value pair, so I don't have to use semicolons here.
You can if you want, but it's not required when you're doing it this way.
So let's run Get‑Process and get the PowerShell
process and save that to the key ps.
Let's skip all of the services, and this time just to keep it interesting,
I'm going to use Get‑CimInstance to retrieve all
instances of the win32_service class, save that to the key services.
You can also create nested hashtables.
So I'm going to create another hashtable here,
has keys of foo, vendor, and color,
and that will then become a nested hashtable in the hashtable $ht.
There is really no end to how far you can go or need to go.
Put in my closing curly brace and that defines then the hashtable $ht.
It took a moment because it had to go and get all of those services.
So what's in $ht now?
Alright, so there is the name, there is my nested property,
the count services, and the PS key.
Let's look at these items a little more closely.
So I have a hashtable that has a key of services and
the value of services is an array, which means I can use array techniques,
such as getting the first five services in that element,
and there we go.
If I want to get the PS key, which is the PowerShell process, there we go.
Again, this is just another object,
and I can continue to use the object.notation.
So if I want to get the CommandLine property of that object,
sure enough, that's pretty simple to do.
Let's look at the nested hashtable.
So there is my necid hashtable.
Again, I can continue to use the dotted notation.
If I want to, I can change the color entry in that nested hashtable to red,
and now that nested hashtable has an updated value.
There is no limit really to what you can do and how
you might want to use a hashtable.
Now, one thing I want to point out, let's go back and look at $ht,
you can see that it's listed here as Name,
nested, account, services, and ps as the keys,
but that is not the order that I created the entries.
Hashtables are unordered,
meaning there is no guarantee that it will be displayed in the same
order that you created or added the key value pairs.
If you need that, there is a way to do that and we're going to do it like this.
You're still going to create a hashtable the same way.
The only difference is we're going to stick a little tag in
front of it called ordered and that tells PowerShell treat
this as a special type of hashtable, an ordered hashtable.
So now if I look at k,
the keys are listed in the same order that I entered them or
created them in the hashtable so I've got first,
second, and third.
Technically, this is a different type of object than the hashtable.
Now you can see this is a ordered dictionary object which is different
technically from a .NET perspective than a hashtable.
However, we still use them the same way.
You can still reference items by their property names,
and if you want, you can change the value.
So nothing really changes in that regard.
The only thing that changes is that the order of the key names is different
if you use ordered versus just a traditional hashtable.
Of course, you should read the help topic about hashtables to learn even more.
Next let me show you some practical ways to use hashtables.
The first step is splatting, and this is how splatting works.
First,
let me show you a command like Get‑Service that you would
normally run and this is using parameters,
‑name and ‑exclude.
So I'm going to get all the services that start with the letter B,
but exclude the bits service.
Now what I'm going to do is define a hashtable and the keys of the hashtable,
Name and Exclude, are going to match the name of the parameters for Get‑Service,
Name and Exclude, and the values, as you can see,
are also identical.
And this splatting technique works whether the parameter is
positional as name is or if it is required for you to type the
name if you want to use it like exclude.
So $p is a hashtable to name value pairs.
The name matches the parameter name and the value
matches the value that you want to give.
So now what I can do is I can run Get‑Service and splat the hashtable.
Now splatting, you can see I get the same result.
The syntax is a little bit different.
Pay close attention here.
Notice I'm using the @ symbol and then the name of
the variable that has my hashtable, $p.
There is no dollar sign, it's just @p,
and so it looks for key names that match up to your parameter names
and then joins those things together and then everything runs the
way that you would normally expect.
Now a reason this is really useful,
and this maybe something that's more important when you get to scripting,
is you can rebuild these hashtables kind of on the fly.
Now I'm going to do this in the console.
Let's create another hashtable and this is a hashtable
that I can use with Get‑CimInstance.
Now in this particular hashtable,
I'm using the ‑verbose parameter because that is a switch that I want to enable.
In a hashtable, I'll use a value of $true, and there you can see the hashtable.
So now I can run Get‑CimInstance and splat $cim.
Notice again, @cim.
I get the verbose output and you can see the results there.
Now though, I can easily modify the hashtable.
Let's say I want to get the win32 operating system.
So if I just change the Classname parameter, everything else remains the same.
And now I can rerun the command, again splatting,
and I get a different result pretty much using the same hashtable.
All I did was just change what needed to be changed.
Again, when you get to scripting,
I think this is a technique that you will be using often.
If you need to though, you can also specify additional parameters.
In this case,
I'm combining the hashtable and then I'm also specifying
an additional parameter ‑ComputerName.
So now I'm acquiring a remote computer to get the operating system information.
Again,
this is something that you will likely use more when you get to scripting and there is a help topic that you should take a look at.