From the course: Learning PowerShell Core

Finding and using modules - PowerShell Tutorial

From the course: Learning PowerShell Core

Start my 1-month free trial

Finding and using modules

- [Instructor] A PowerShell module is made up of two main elements, the module itself, which is a PSM1 file containing commands, and a manifest file, which contains metadata about the module. Every command in PowerShell is part of a module. The module file has a PSM1 extension, but it's actually just a typical PowerShell script like a PS1 file. The module is the package that contains one or more commands. The manifest is like a shipping manifest that outlines information about the module and the commands therein. Once the module is on a machine, it is imported into the current session and the commands are used, as you'll see in the upcoming demo. So I'm now on my PowerShell Core instance, and to understand modules, let's first take a peek at the modules that are loaded by default with PowerShell Core. I can run the Get-Command command and pipe this to more so we get a page-by-page output. You can see that each command here is part of a module. So notice on the right-hand side the source. We have Appx, Storage, SmbWitness, et cetera. Those are all the modules that those commands are in. I'll hit Q here to come out of the more view, and let's say that you want to find all the commands inside of a module. To do that, I can run Get-Command again and then provide the module parameter, and then I'll pick the module, so I'll just pick PSDiagnostics for now. So notice that we can limit that output of that Get-Command return by specifying the module, which is PSDiagnostics in this example, and it will tell you all the commands that are inside of that particular module. So to find all of the module that are available in the system, you can run the Get-Module command, and the Get-Module returns a few different modules that are loaded in the current session. However, know that Get-Module doesn't return all the modules that are currently available to you. Modules are located on the file system in a PSM1 file. When you run Get-Module without any parameters, it will only provide you with a listing of all the modules that are currently imported in your current session. To find all of the modules that are available to you on the file system and imported, I'll hit the up arrow here and choose Get-Module, and then I'll provide the ListAvailable parameter. When I do that, you'll see that it returns a lot more modules because it is now looking at the file system, looking at all available that PowerShell currently knows about. If you'd like to find out the location of some of these modules, what we can do is I can use the Get-Module command, push up arrow here to get that, and then let's say that I want to pick one, I don't know, I'll pick one just called PowerShellGet. And again with the formattings, and to find all of the properties on this, I can pipe this to Select-Object -Property and put a star. As you can see, all the different information, different object properties available on this module. To show the path, I can select just the path of the property, and now you can see that this module in particular is stored at C:\program files\powershell\6\Modules\PowerShellGet. So right now we're working with a few different modules that are currently loaded onto our system. So let's first check and hit up arrow here and check and see which modules are available. So looks like one of 'em is called CallPass. I don't know what that is, so let's say that I want to remove that from my current session. I want to remove that completely from memory. It won't remove it from the file system, but it will remove it from my current session. To do that, I can run Remove-Module, provide it the name of the module, and then I'll choose the name. And now when I up arrow again and see Get-Module, now you can see that it's gone. Remove-Module doesn't remove from the file system. Remove-Module removes it from the current session. Now let's say that you have a module that is maybe not located in a place that PowerShell natively understands, or maybe you would like to just import it manually without the PowerShell's auto-importing mechanism working. To do that, you can run Import-Module. So let's say that I want to import the CallPass module again. To do that, I would run Import-Module, provide the name of CallPass, and now I will hit up arrow again, check Get-Module, and now you can see that it's imported again. So by default, if PowerShell knows about the module, it will auto-import it when you try to run a command, or you can manually remove and import modules with the Remove-Module and the Import-Module command. So let me go ahead and paste this line in here, and this is just an example of one of the module manifests that are currently on the machine called PSReadLine. Now, don't pay attention to the code I actually ran to get this. I don't expect you to know that. But what this is essentially doing is just reading the text in that PSD1 file for the PSReadLine module. Notice there on the top that we have an asterisk and then a left curly brace, and then at the bottom here, you see a right curly brace. That is a PowerShell hash tail. It is actually nested inside of this PSD1 file. This is a PowerShell manifest. All manifests have a key value pair, so we have root module, GUID, author, company name, and then there's a value for each of those. Those are called key value pairs. And every module that has an associated manifest will have this general metadata information about the module. So that's been an introduction to finding and using modules in PowerShell.

Contents