As I've mentioned a couple of times now,
authors of PowerShell code will often
release updates to their modules. This may
be to fix bugs or introduce new
functionality or features. Like any
software you install, you'll want to make
sure that you do regular maintenance on it
to keep it patched and up to date. Let's
dive into doing that with PowerShell
modules. I'm going to install a different
module from the PowerShell Gallery named
posh‑ssh, which is a popular module for
SSH and SCP connections to endpoints, and
I'm going to refer to it moving forward
simply as posh‑ssh. I happen to know that
the most recent version on the gallery is
2.2, but I'm going to use the required
version parameter to install version 2.0
on my computer. The module that we're
working with here itself isn't too
important. Just pay attention to the
versions in the next few exercises. I'll
say yes again to the prompt about the PS
Gallery not being trusted. And actually,
while I remember, let me just run
Set‑PSRepository to set the gallery to a
trusted repo, so we stopped getting that
prompt. I can confirm that the posh‑ssh
module has been installed by running
Get‑Module posh‑ssh ‑ListAvailable. And
yep, all good. The command we need to use
to update a module, shockingly, is named
Update‑Module. As per the other commands,
let's quickly check out the help for
Update‑Module. And this command actually
doesn't have any required parameters. I'm
not going to do this, but if you run
Update‑Module with no parameters, it will
attempt to update all modules on this
computer that have been installed via the
PackageManagement Framework. Let's run
Update‑Module for the posh‑ssh module, but
let's use the RequiredVersion parameter
again to specifically update to version
2.1. I'll go back to check on that by
running Get‑Module, and this is another
important thing. I want you to be aware
of. Updating a module does not remove the
old version of that module. You need to
explicitly uninstall the old version of a
module using another command that we'll
look at in the next clip. I'm going to say
that again because it trips a lot of
people up. Updating a module does not
remove the old version of that module.
Finally, if I do one last Update‑Module of
posh‑ssh, but this time I won't specify a
version, PowerShell will go and get the
latest version of that module for me. If
we check out Get‑Module for posh‑ssh
again, we'll now see. 3 versions are
installed, and version 2.2 is the latest
version that was available on the
PowerShell Gallery. If I run Import‑Module
posh‑ssh and use the verbose parameter and
scroll back up to the top, you'll see here
that even though several versions are
installed on my machine, it is loading the
latest version of 2.2 by default into my
session. To wrap up here with
Update‑Module, there's another behavior I
want you to be aware of, and again, this
is important. Update‑Module will only
update PowerShell modules that have been
installed with the PowerShell GET command
Install Module. Remember that all of this
is built on top of that PackageManagement
Framework, right? So if a module wasn't
installed using that framework, it can't
be updated by using that framework.
There's a command that's a part of
PowerShell GET named Get‑InstalledModule,
which shows all modules that are installed
on a computer using PowerShell GET. I'm
going to run that inside of parentheses
and then look at the count property to get
the total number of installed modules,
which is two. That would be the two
modules we just installed, PS Slack and
posh‑ssh. However, if I do the same thing,
but run Get‑Module ‑ListAvailable, there
are 78 PowerShell modules that exist on
this computer. It just so happens that
only two of them have been installed with
the PackageManagement Framework. If I
attempted to update a module that hasn't
been installed using the PackageManagement
Framework, such as the SmbShare module, it
will helpfully tell us that it wasn't
installed using the Install‑Module
command, so it cannot be updated. If you
wanted to be able to update this module,
you'd first need to remove it from this
system and then install it by using
Install‑Module, so it becomes managed by the PackageManagement Framework.