Using PowerShell Modules

*If you're just joining us, this post is part of a 12-part series on Getting Started with PowerShell. If you'd like to catch up (or skip ahead - more posts coming soon), click on the appropriate post below.
Getting Started with PowerShell
Windows PowerShell, PowerShell Core and PowerShell: Huh?
Installing PowerShell and Visual Studio Code
Running PowerShell Commands and Getting Help
Working with the PowerShell Pipeline
Writing your First PowerShell Script
Understanding Loops in PowerShell

"Just download and import this module", "Remove this module from your session", "The PowerShell Gallery has tons of community modules"; have you heard these statements from others and wonder what in the world they're talking about? If so, let's get you up to speed.

You probably know by now PowerShell has many different commands. Commands are the lifeblood of PowerShell and allow you to automate so many things. There are thousands of available commands out there. To organize and manage all of these cmdlets, we use modules.

Every command is in a module. Modules are groups of commands brought together under one, central theme. In this article, you're going to learn how to manage local modules, download new ones and finally wrap your head about the concept of a module.

Installing New Modules

One of the most useful features of a module is its portability. Modules full of commands can be shared amongst your team or thousands of people. One of the first encounters you may have with modules is downloading and install new ones.

Perhaps you're ready to start automating all the things in Azure in PowerShell. Before you can start, you'll need a module or modules to do so. Why? Because PowerShell does not come with Azure support or any other service for that matter.

When you install PowerShell, you get a few hundred cmdlets that assist you in doing basic tasks like reading files, creating registry keys, pinging computers, connecting to remote systems and so on. The out-of-the-box PowerShell cmdlet can, in no way, support all that you can do with the language. That's up to module designers and builders.

To perform tasks outside of the built-in PowerShell commands, you must install modules that contain commands that give you that functionality.

The Azure PowerShell Module

Let's take Azure tasks, for example. To work with Azure in PowerShell, you need the set of modules known as Az. The Az collection of modules allow you to work with nearly every Azure resource out there.

To download and install the Az PowerShell modules, you can use the PowerShell Gallery. The PowerShell Gallery is a public, online repository of modules that you can download, install and begin using immediately.

The Az module is a little bit different than most modules. Technically, the Az module is a set of modules all bundled as one. You'll see this in the next section.

One of the best ways to find and download modules from the PowerShell Gallery is using the *-Module commands built into PowerShell.

Knowing that you need the Az modules, use the Find-Module command to search the PowerShell Gallery. You can see below that a single entry was returned.

PowerShell Find-Module output on screen

Now that you know the Az module is available on the PowerShell Gallery, you need to download and install it onto your system. One way to do that is to pipe the Az module to the Install-Module cmdlet.

Find-Module -Name Az | Install-Module

After a minute or so, PowerShell should have download and installed this module and all of the child modules that come with the Az module.

Inspecting PowerShell Modules

Now that you have the Az module installed, let's see what you got.

To inspect modules on your system, use the Get-Module command. This command lists all of the modules that both imported into your session and on the local filesystem.

Run Get-Module -Name Az and see what happens.

Nothing. Nothing is returned. But what gives? We just installed it?

The Az module is there but not imported. To use the commands in a module, the module must be imported first. To find all modules available on the local filesystem, use the ListAvailable parameter. When you run this command, you will see something similar to the below screenshot.

Get-Module -Name Az -ListAvailable
PowerShell Comman Get-Module List-Available

The example system has two versions of the Az module installed. If this is the first time you're installing the Az module, you will only see one.

So you've confirmed the module is on your system but it's just not imported yet. Let's dig deeper and import the Az module. To do so, run the Import-Module command. This commands reads the module residing on the local filesystem and brings it into the current PowerShell session.

Import-Module Az

Now run Get-Module -Name Az again and you'll see the Az module show up. The Az module and all of the commands within are now available.

If you already know the commands to run inside of a module, you do not have to manually import it with Import-Module. PowerShell will automatically import the module.

Inspecting Commands in Modules

You now have the Az module download, installed and imported into a PowerShell session. You can now look inside the module to discover what commands are available. To discover what commands are inside of a module, use the Get-Command command using the Module parameter.

Get-Command -Module Az

Nothing is returned again. Why? Nothing is returned because the Az module doesn't contain any commands! Remember when I mentioned in the earlier section about the Az module being a unique case? This is it.

The Az module itself is a "wrapper" to group together all of the other Azure modules with commands inside. Run Get-Module -Name Az.*. The Name parameter of Get-Module accepts wildcards. All Azure modules are under the Az module as Az.[service].

Get-Module -Name AZ with wildcard to return all Azure services

Perhaps you'd like to work with Azure virtual machines. All of the virtual machine cmdlets exist in the Az.Compute module. Try out Get-Command again but this time, specify Az.Compute instead of Az. You will now see over 200 cmdlets.

Get-Command -Module Az.Compute
Output of Get-Command -Module Az.Compute

At this point, you can run any of the commands inside of any of the Az modules to automate your way to Azure greatness!

Summary

You can think of PowerShell modules as simply groups of commands brought together under a common theme. You can find modules created by the PowerShell community and other companies for free in the PowerShell Gallery online repository.

Using the various *-Module commands in PowerShell, you can download, install, inspect and also remove modules at your discretion.

 
Adam Bertram

About the Author

Hi, I'm Adam. I'm a 20+ year veteran of IT and experienced online business professional. I'm an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer, author and content marketing writer for multiple technology companies.

Follow me on my blog, Twitter, LinkedIn

 

Previous
Previous

Connecting to Azure

Next
Next

New and Improved method of integrating Azure AD in Azure Kubernetes Service (AKS) - Preview