Core Skills Level: Intermediate Chapter 6: Toolboxes You Can Load When You Need Them
What is a module
A module bundles related commands together. Instead of loading every command at startup, PowerShell loads modules on demand, which keeps startup fast and the command list manageable.
Lesson 1 of 2 in Chapter 6: Toolboxes You Can Load When You Need Them. Previous step in this path: Your first script and data types. 3 learning objectives, 7 examples, and a knowledge check.
Start: what you will learn
A module bundles related commands together. Instead of loading every command at startup, PowerShell loads modules on demand, which keeps startup fast and the command list manageable.
Learning objectives
- Explain what a module contains
- List available modules with Get-Module
- Find which module provides a command
Concept in simple words
What it is: a module is a package of related PowerShell commands, focused on one area. Think of it as one toolbox for one kind of job. The directory module, for example, holds commands for working with directory users. The network modules hold commands for network tasks. You load only the packages you need, when you need them.
What lives inside a module: a module is a self contained, reusable unit. It can hold cmdlets, functions, variables, and other resources such as help and formatting information. A cmdlet is a built in compiled command. A function is a named block of PowerShell code that you can call by name. A module groups those pieces together so they travel together, load together, and can be found together.
Why it works
Why it works this way: loading every command at startup would be slow, and it would flood you with names you do not need. By grouping commands into modules, PowerShell can start fast and load a package on demand, when you first use one of its commands or when you import it yourself. That keeps the command list manageable and your session tidy. You get a small, clear set of tools for the job in front of you, not the whole workshop at once.
Think of it like this
Think of it like this: a module is a toolbox for one kind of job. You would not carry every toolbox in the workshop to fix one tap, and PowerShell does not load every module to run one command. You pick up the toolbox for the job, use its tools, and put it back when you are done. The toolbox itself stays in the workshop, ready for next time.
The three words you must keep apart: installed, loaded, and available. Installed means the module files are on the computer. Loaded means the module is active in your current session, so you can use its commands right now. Available is the word used by Get-Module -ListAvailable, and it means installed and found in the module search path, whether or not it is loaded yet. A module can be installed and available, but not loaded. That is normal, and it is not an error.
Where PowerShell looks: PowerShell finds modules in the folders listed in the $env:PSModulePath variable. That variable is simply a list of folder paths. When you run Get-Module -ListAvailable, PowerShell scans those folders and reports what it finds there. A module stored somewhere else, outside those folders, will not appear in that list, even if the files are on the computer. That is why location matters as much as installation.
How PowerShell finds a command without loading everything: when PowerShell starts, it builds a picture of which commands live in which modules in those folders. That is why you can ask Get-Command about a command and learn its module, even before the module is loaded. PowerShell knows where the tool lives, without opening every toolbox first. This discovery step is read only. It changes nothing in your session, and it is safe to practise.
Automatic loading, step by step: since PowerShell 3.0, an installed module in the module path is usually loaded automatically the first time you use one of its commands. You type the command, PowerShell finds its module, loads that module into your session, and then runs the command. The same automatic load can happen when you ask Get-Command for the exact name of a command, or when you ask Get-Help for that command. You do not always have to import by hand, but an explicit import is still clearer in a script, because the script then says plainly what it needs.
One careful exception to automatic loading: if you use Get-Command with a wildcard character, such as an asterisk, PowerShell does not import modules. It treats a wildcard search as discovery only. So Get-Command Get-* will show you names without loading every matching module, while Get-Command Get-ADUser, with the exact name, can trigger the load of its module. If a command seems to appear in discovery but not in your loaded list, that difference is often the reason.
Versions, in plain words: the same module can exist in more than one version on one computer. Each version is a separate set of files, and Get-Module -ListAvailable can show more than one row for the same module name, one row per version. That is useful when a newer version changes a command, and you still want the older behaviour for one task. You can narrow the list by name, and you can ask for a particular version when you import, which the next lesson covers.
Module shapes, in plain words: most modules you meet as a learner are script modules, built from PowerShell code in a .psm1 file, or manifest modules, described by a .psd1 file. Some modules are binary modules, built from compiled code. You do not need to build a binary module in this chapter. What matters now is the idea behind all three shapes: the module is the package, the commands inside are the tools, and the manifest, when one exists, is the label that describes the package.
How it works, step by step
How it works, step by step, in order: first, list what is already loaded with plain Get-Module, which shows only the current session. Second, list what is installed with Get-Module -ListAvailable, which shows modules on the computer even if they are not loaded yet. Third, if a command you want is missing, find its module with Get-Command and its ModuleName property. Fourth, list the commands inside that module with Get-Command -Module, so you can see what the toolbox actually contains. Fifth, import that module, or install it first if it is not on the computer at all. Sixth, check again with plain Get-Module to confirm the import worked.
Worked walkthrough, example 1: in Get-Module -ListAvailable | Select-Object Name, Version -First 5, Get-Module -ListAvailable lists installed modules. The pipe sends those module objects onward. Select-Object keeps only Name and Version, and -First 5 keeps the first five rows. Look at the output and check whether the module you need is listed at all, because that tells you whether to import it or install it. If the list is long, do not scroll through it by eye. Narrow it with -Name, as in a later example.
Worked walkthrough, example 2: in Get-Command Get-ADUser | Select-Object Name, ModuleName, Get-Command asks about one exact command, Get-ADUser. Select-Object keeps only Name and ModuleName. ModuleName is the answer to the question, which toolbox holds this tool. Look for the module name in that column. If you see ActiveDirectory there, you know what to import. If you change Get-ADUser to another command name, the ModuleName column changes to that command owner, and the rest of the command stays the same.
Worked walkthrough, example 3: in Get-Module | Select-Object Name, Version, plain Get-Module with no extra parameter lists only the modules already loaded in your current session. The pipe sends those module objects onward. Select-Object keeps only Name and Version, so the list is short and easy to scan. Compare this with -ListAvailable, which shows installed modules whether or not they are loaded. A short loaded list next to a long available list is normal. It means PowerShell is keeping your session light.
Worked walkthrough, example 4: in Get-Module -ListAvailable -Name Microsoft.PowerShell.Management | Select-Object Name, Version, Path, the -Name parameter narrows the scan to one module name, instead of listing everything. Select-Object then keeps Name, Version, and Path. Path tells you where the module files live on disk, which is useful when two versions exist, or when you want to check that you are looking at the right copy. If you change -Name to another module name, you get that module rows instead, and nothing else changes.
Worked walkthrough, example 5: in Get-Command -Module Microsoft.PowerShell.Management | Select-Object Name, CommandType -First 5, the -Module parameter asks for the commands inside one module. Select-Object keeps Name and CommandType, and -First 5 keeps the first five rows. CommandType tells you whether each entry is a cmdlet, a function, or another kind of command. Look at the names first, and use them as a menu of what the toolbox can do, before you run any of them.
Worked walkthrough, example 6: in $env:PSModulePath, you are simply asking PowerShell to show the value of a variable. That variable holds the list of folders PowerShell searches for modules. This command changes nothing, it only reads. Look at the folders in the result and notice that they are the places Get-Module -ListAvailable scans. If a module folder is not in this list, Get-Module -ListAvailable will not find it, and automatic loading will not find it either.
Worked walkthrough, example 7: in (Get-Command Get-Date).ModuleName, the parentheses run Get-Command Get-Date first, and then .ModuleName reads one property from the result. The output is a single module name, not a table. That is handy when you only want the owner name, for example to paste it into an Import-Module command. If you remove the parentheses and the property, you get the full command information instead, which is more detail than you need for this one question.
When to use it
When to use it, and when not to: use modules whenever a task belongs to a clear area, such as directory work or networking, because the module gathers the right tools in one place. Use discovery commands freely, because listing and inspecting change nothing. Do not assume PowerShell is missing a command just because it is not loaded yet. Check the installed list first, and remember that installing a module and importing a module are two different steps. Installing puts files on the computer. Importing loads them into your session.
Edge cases to watch for: first, a module outside the module path is invisible to -ListAvailable, so you must import it by full path, which the next lesson shows. Second, two modules can provide commands with the same name, so the command that runs may not be the one you expected. The next lesson shows how to handle that clash. Third, a command that only appears with a wildcard search has not loaded its module yet, so do not be surprised if plain Get-Module does not list it right after a wildcard search.
Worked examples
Read each example in order. Each one explains its parts, then shows what to look for in the output.
Syntax
Get-Module -ListAvailableExample 1
Lists installed modules with versions. Loaded or not, they appear here if they are installed in the module path.
Get-Module -ListAvailable | Select-Object Name, Version -First 5Illustrative output: Name Version ---- ------- ActiveDirectory 1.0.0.0
Example 2
Shows which module provides Get-ADUser, so you know what to import.
Get-Command Get-ADUser | Select-Object Name, ModuleNameIllustrative output: Name ModuleName ---- ---------- Get-ADUser ActiveDirectory
Example 3
Get-Module on its own lists only the modules already loaded in your current session. The pipe sends those module objects onward. Select-Object keeps only Name and Version, so the list is short and easy to scan. Compare this with -ListAvailable, which shows installed modules whether or not they are loaded.
Get-Module | Select-Object Name, VersionIllustrative output: Name Version ---- ------- Microsoft.PowerShell.Management 3.1.0.0
Example 4
Narrows the installed list to one module name, and shows where its files live. Use this when you want one module, not the whole list.
Get-Module -ListAvailable -Name Microsoft.PowerShell.Management | Select-Object Name, Version, PathIllustrative output: Name Version Path ---- ------- ---- Microsoft.PowerShell.Management 3.1.0.0 C:\Program Files\PowerShell\Modules\Microsoft.PowerShell.Management
Example 5
Lists the first five commands inside one module, with their type. Use this as a menu before you run anything from the module.
Get-Command -Module Microsoft.PowerShell.Management | Select-Object Name, CommandType -First 5Illustrative output: Name CommandType ---- ----------- Get-Process Cmdlet Get-Service Cmdlet Stop-Process Cmdlet
Example 6
Shows the folders PowerShell searches for modules. This only reads a variable, it changes nothing.
$env:PSModulePathIllustrative output: C:\Users\You\Documents\PowerShell\Modules;C:\Program Files\PowerShell\Modules
Example 7
Returns only the module name for Get-Date, with no table. Handy when you just need the owner name for an import.
(Get-Command Get-Date).ModuleNameIllustrative output: Microsoft.PowerShell.Utility
Nothing on this page executes PowerShell code. The outputs above are illustrative only.
Common mistakes and troubleshooting
Read these before you try the examples, so you can spot the same problems in your own work.
- Assuming a command is missing from PowerShell when its module simply is not loaded yet.
- Confusing installing a module with importing it. Install puts it on the computer. Import loads it into your session.
- Installing the same module again every session instead of simply importing the one that is already installed. Install puts a module on the computer once, while import loads it into the session you are working in.
- Reading plain Get-Module as the full installed list. Plain Get-Module shows only loaded modules. Use -ListAvailable for the installed list.
- Expecting Get-Module -ListAvailable to find a module stored outside the module path. It scans only the folders in $env:PSModulePath, so a module elsewhere needs an import by full path.
- Expecting a wildcard Get-Command search to load modules. A wildcard search is discovery only, and it does not import the matching modules.
- Forgetting that the same module name can appear in more than one version. Check the Version column before you assume two rows are duplicates.
- Judging a module by its name alone, without listing its commands. Use Get-Command -Module to see what the toolbox actually contains before you use it.
- Running a command from a module you have not inspected, when a safer read only listing would have answered the question first.
Knowledge check
Knowledge check
What does Get-Module -ListAvailable show?
Pick one answer. Feedback appears right away, and you can try again.
Remember this and next step
Remember this: installed is not the same as loaded. -ListAvailable shows what is on the computer in the module path, plain Get-Module shows what is in your session, ModuleName tells you which package a command belongs to, and automatic loading means a module in the path often loads itself the first time you use its command. Start with discovery, then import only what the job needs, and your sessions will stay fast, tidy, and easy to understand.
Next step: Import a module and build a custom module