Skip to content

Commit 5c7c7b6

Browse files
authored
Clean up docs on PSModulePath (#11454)
1 parent 18855fb commit 5c7c7b6

File tree

11 files changed

+716
-674
lines changed

11 files changed

+716
-674
lines changed

reference/5.1/Microsoft.PowerShell.Core/About/about_Modules.md

Lines changed: 89 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Explains how to install, import, and use PowerShell modules.
33
Locale: en-US
4-
ms.date: 08/09/2024
4+
ms.date: 10/09/2024
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_modules?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Modules
@@ -18,29 +18,24 @@ PowerShell are implemented as scripts, functions, or cmdlets. The language
1818
includes [keywords][07], which provide the structure and logic of processing,
1919
and other resources, such as variables, providers, aliases.
2020

21-
A module is a self-contained, reusable unit that can include cmdlets, providers,
22-
functions, variables, and other resources that can be imported into a PowerShell
23-
session or any custom PowerShell program.
24-
25-
Before the functionality contained in a module is usable, the module must be
26-
loaded into the PowerShell session. By default, PowerShell automatically loads
27-
an installed module the first time you use a command from the module. You can
28-
configure automatic module loading behavior using the variable
29-
`$PSModuleAutoloadingPreference`. For more information, see
21+
A module is a self-contained, reusable unit that can include cmdlets,
22+
providers, functions, variables, and other resources. By default, PowerShell
23+
automatically loads an installed module the first time you use a command from
24+
the module. You can configure automatic module loading behavior using the
25+
variable `$PSModuleAutoloadingPreference`. For more information, see
3026
[about_Preference_Variables][08].
3127

32-
You can also manually unload or reload modules during a PowerShell session. To
33-
unload a module, use the `Remove-Module` cmdlet. To load or reload a module,
34-
use `Import-Module`.
28+
You can also manually load or unload modules during a PowerShell session. To
29+
load or reload a module, use `Import-Module`. To unload a module, use the
30+
`Remove-Module` cmdlet.
3531

36-
PowerShell comes with a base set of modules. Anyone can create new PowerShell
37-
commands or other resources, and publish them as modules that users can install
38-
as needed.
32+
PowerShell includes a base set of modules. Anyone can create new modules using
33+
C# or the PowerShell scripting language itself. Modules written in C# as
34+
compiled .NET assemblies are known as native modules. Modules written in
35+
PowerShell are known as script modules.
3936

40-
You can write modules in C# as compiled .NET assemblies, known as native
41-
modules, or in plain PowerShell, known as script modules. This topic explains
42-
how to use PowerShell modules. For information about how to create PowerShell
43-
modules, see [Writing a PowerShell Module][02].
37+
This article explains how to use PowerShell modules. For information about how
38+
to create PowerShell modules, see [Writing a PowerShell Module][02].
4439

4540
> [!NOTE]
4641
> Prior to PowerShell 3.0, cmdlets and providers were packaged in PowerShell
@@ -49,42 +44,13 @@ modules, see [Writing a PowerShell Module][02].
4944
> remaining in PowerShell. All other snap-ins were converted to modules.
5045
> Creation of new snap-ins is no longer supported.
5146
52-
## Install a published module
47+
## Default module locations
5348

54-
A published module is a module that is available from a registered repository,
55-
such as the PowerShell Gallery. The **PowerShellGet** and
56-
**Microsoft.PowerShell.PSResourceGet** modules provide cmdlets for finding,
57-
installing, and publishing PowerShell modules to a registered repository.
49+
PowerShell stores modules in the following default locations:
5850

59-
The **PowerShellGet** module is included with PowerShell 5.0 and later
60-
releases. The **Microsoft.PowerShell.PSResourceGet** module is included with
61-
PowerShell 7.4 and later releases. **Microsoft.PowerShell.PSResourceGet** is
62-
the new preferred package manager for PowerShell and can be installed on
63-
previous versions of PowerShell. Use the `Install-Module` or
64-
`Install-PSResource` cmdlet to install modules from the PowerShell Gallery.
65-
66-
```powershell
67-
Get-Command Install-Module, Install-PSResource
68-
```
69-
70-
```Output
71-
CommandType Name Version Source
72-
----------- ---- ------- ------
73-
Function Install-Module 2.9.0 PowerShellGet
74-
Cmdlet Install-PSResource 1.0.0 Microsoft.PowerShell.PSResourceGet
75-
```
76-
77-
For more information, see [PowerShellGet Overview][01].
78-
79-
## Manually install a module
80-
81-
If you receive a module as a folder with files in it, you need to install it on
82-
your computer before you can use it in PowerShell.
83-
84-
PowerShell comes with several preinstalled modules. On Windows-based computers,
85-
many Windows features include modules for managing the feature. Those modules
86-
get installed when the feature is installed. Other modules may come in an
87-
installer or setup program that installs the module.
51+
- All users scope - `$env:ProgramFiles\WindowsPowerShell\Modules`
52+
- Current user scope - `$HOME\Documents\WindowsPowerShell\Modules`
53+
- Modules shipped with PowerShell - `$PSHOME\Modules`
8854

8955
By default, the `Modules` folder for the current user doesn't exist. If you
9056
installed a module in the `CurrentUser` scope using `Install-Module` or
@@ -97,31 +63,19 @@ Use the following command to create a `Modules` folder for the current user:
9763
$folder = New-Item -Type Directory -Path $HOME\Documents\WindowsPowerShell\Modules
9864
```
9965

100-
Copy the entire module folder into the new created folder. In PowerShell use
101-
the `Copy-Item` cmdlet. For example, run the following command to copy the
102-
`MyModule` folder from `C:\PSTest` to the folder you just created:
103-
104-
```powershell
105-
Copy-Item -Path C:\PSTest\MyModule -Destination $folder
106-
```
107-
108-
You can install a module in any location, but installing your modules in a
109-
default module location makes them easier to manage. For more information about
110-
the default module locations, see [about_PSModulePath][10].
66+
These locations are automatically included in the `$env:PSModulePath`
67+
environment variable. For more information about the default module locations,
68+
see [about_PSModulePath][10].
11169

11270
## Module autoloading
11371

11472
The first time that you run a command from an installed module, PowerShell
11573
automatically imports (loads) that module. The module must be stored in the
116-
locations specified in the `$env:PSModulePath` environment variable. Modules in
117-
other locations must be imported using the `Import-Module` cmdlet.
74+
locations specified in the `$env:PSModulePath` environment variable.
11875

11976
Module autoloading allows you to use commands in a module without any setup or
120-
profile configuration. There's no need to manage modules after you install them
121-
on your computer.
122-
123-
Each of the following examples cause the **CimCmdlets** module, which contains
124-
`Get-CimInstance`, to be imported into your session.
77+
profile configuration. Each of the following examples causes the **CimCmdlets**
78+
module, which contains `Get-CimInstance`, to be imported into your session.
12579

12680
- Run the Command
12781

@@ -141,17 +95,9 @@ Each of the following examples cause the **CimCmdlets** module, which contains
14195
Get-Help Get-CimInstance
14296
```
14397

144-
You can use the `Get-Command` cmdlet list the commands in all installed
145-
modules, even if they're not yet in the session. When you use `Get-Command`
146-
with a wildcard character (`*`), PowerShell doesn't import any modules. You can
147-
use wildcards for command discovery without loading modules that you may not
148-
need in your session.
149-
150-
Also, commands that use PowerShell providers don't automatically import a
151-
module. For example, if you use a command that requires the `WSMan:` drive,
152-
such as the `Get-PSSessionConfiguration` cmdlet, you might need to run the
153-
`Import-Module` cmdlet to import the **Microsoft.WSMan.Management** module that
154-
includes the `WSMan:` drive.
98+
When you use `Get-Command` with a wildcard character (`*`), PowerShell doesn't
99+
import any modules. You can use wildcards for command discovery without loading
100+
modules that you might not need in your session.
155101

156102
## Manually import a module
157103

@@ -160,16 +106,21 @@ locations specified by the `$env:PSModulePath` environment variable, or when
160106
the module is provided as a standalone `.dll` or `.psm1` file, rather than a
161107
packaged module.
162108

109+
Also, commands that use PowerShell providers don't automatically import a
110+
module. For example, if you use a command that requires the `WSMan:` drive,
111+
such as the `Get-PSSessionConfiguration` cmdlet, you might need to run the
112+
`Import-Module` cmdlet to import the **Microsoft.WSMan.Management** module that
113+
includes the `WSMan:` drive.
114+
163115
You might also want to change how the module is imported in your session. For
164116
example, the **Prefix** parameter of `Import-Module` adds a distinctive prefix
165117
to the noun portion of the cmdlets imported from the module. The **NoClobber**
166118
parameter prevents the module from adding commands that would hide or replace
167119
existing commands in the session. For more information, see
168120
[Manage name conflicts][03].
169121

170-
You can import a module that's installed in your `$env:PSModulePath` by
171-
specifying the module name. For example, the following command imports the
172-
**BitsTransfer** module into the current session.
122+
The following example imports the **BitsTransfer** module into the current
123+
session.
173124

174125
```powershell
175126
Import-Module BitsTransfer
@@ -202,10 +153,56 @@ the `Import-Module` command to your PowerShell profile.
202153

203154
For more information about profiles, see [about_Profiles][09].
204155

156+
## Install a published module
157+
158+
A published module is a module that's available from a registered repository,
159+
such as the PowerShell Gallery. The **PowerShellGet** and
160+
**Microsoft.PowerShell.PSResourceGet** modules provide cmdlets for finding,
161+
installing, and publishing PowerShell modules to a registered repository.
162+
163+
The **PowerShellGet** module is included with PowerShell 5.0 and later
164+
releases. The **Microsoft.PowerShell.PSResourceGet** module is included with
165+
PowerShell 7.4 and later releases and is the preferred package manager for
166+
PowerShell. **Microsoft.PowerShell.PSResourceGet** can be installed, side by
167+
side with **PowerShellGet**, on older versions of PowerShell. Use the
168+
`Install-Module` or `Install-PSResource` cmdlet to install modules from the
169+
PowerShell Gallery.
170+
171+
```powershell
172+
Get-Command Install-Module, Install-PSResource
173+
```
174+
175+
```Output
176+
CommandType Name Version Source
177+
----------- ---- ------- ------
178+
Function Install-Module 2.9.0 PowerShellGet
179+
Cmdlet Install-PSResource 1.0.0 Microsoft.PowerShell.PSResourceGet
180+
```
181+
182+
For more information, see [PowerShellGet Overview][01].
183+
184+
## Manually install a module
185+
186+
You can manually install a module by copying the module contents from another
187+
folder. That folder can be in another location on the local machine or
188+
installed on another machine. To install a module manually, copy the entire
189+
module folder into a new location included in your `$env:PSModulePath`.
190+
191+
In PowerShell use the `Copy-Item` cmdlet. For example, run the following
192+
command to copy the `MyModule` folder from `C:\PSTest`:
193+
194+
```powershell
195+
$modulePath = $HOME\Documents\PowerShell\Modules\MyModule
196+
Copy-Item -Path C:\PSTest\MyModule\* -Destination $modulePath -Recurse
197+
```
198+
199+
You can install a module in any location, but installing your modules in a
200+
default module location makes them easier to manage.
201+
205202
## Find installed modules
206203

207-
The `Get-Module` cmdlet gets the PowerShell modules that have been imported in
208-
your session.
204+
The `Get-Module` cmdlet gets the PowerShell modules that are loaded in the
205+
current PowerShell session.
209206

210207
```powershell
211208
Get-Module
@@ -312,7 +309,7 @@ have the same names as commands or items in the session.
312309
current session. Name conflicts can result in commands being hidden or
313310
replaced. Command replacement occurs when the imported module contains a
314311
command with the same name as an existing command in the session. The newly
315-
imported command replaces takes precedence over the existing command.
312+
imported command takes precedence over the existing command.
316313

317314
For example, when a session includes a function and a cmdlet with the same
318315
name, PowerShell runs the function by default. When the session includes
@@ -322,9 +319,9 @@ name, by default, it runs the most recently added command.
322319
For more information, including an explanation of the precedence rules and
323320
instructions for running hidden commands, see [about_Command_Precedence][04].
324321

325-
You can run a command that has been hidden or replaced by qualifying the
326-
command name. To qualify the command name, add the name of module than contains
327-
the version of the command you want. For example:
322+
You can run a hidden or replaced command by qualifying the command name. To
323+
qualify the command name, add the name of module that contains the version of
324+
the command you want. For example:
328325

329326
```powershell
330327
Microsoft.PowerShell.Utility\Get-Date
Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,37 @@
11
---
22
description: The PSModulePath environment variable contains a list of folder locations that are searched to find modules and resources.
33
Locale: en-US
4-
ms.date: 03/24/2023
4+
ms.date: 10/09/2024
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_PSModulePath?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_PSModulePath
88
---
99
# about_PSModulePath
1010

1111
## Short description
12+
1213
This article describes the purpose and usage of the `$env:PSModulePath`
1314
environment variable.
1415

1516
## Long description
1617

1718
The `$env:PSModulePath` environment variable contains a list of folder
18-
locations that are searched to find modules and resources. PowerShell
19-
recursively searches each folder for module (`.psd1` or `.psm1`) files.
20-
21-
`Install-Module` has a **Scope** parameter that allows you to specify whether
22-
the module is installed for the current user or for all users. For more
23-
information, see [Install-Module][01].
19+
locations. PowerShell recursively searches each folder for module (`.psd1` or
20+
`.psm1`) files.
2421

2522
By default, the effective locations assigned to `$env:PSModulePath` are:
2623

27-
- **System-wide locations**: These folders contain modules that ship with
28-
PowerShell. These modules are stored in the `$PSHOME\Modules` folder. This is
29-
also the location where the Windows management modules are installed.
30-
31-
Modules installed in the **AllUsers** scope are stored in
32-
`$env:ProgramFiles\WindowsPowerShell\Modules`.
33-
34-
- **User-installed modules**: These are modules installed in the
35-
**CurrentUser** scope. The location of the **CurrentUser** scope is typically
36-
the `$HOME\Documents\WindowsPowerShell\Modules` folder. The specific location
37-
of the `Documents` folder varies by version of Windows and when you use
38-
folder redirection. Also, Microsoft OneDrive can change the location of your
39-
`Documents` folder. You can verify the location of your `Documents` folder
40-
using the following command: `[Environment]::GetFolderPath('MyDocuments')`.
41-
42-
- **Application specific modules**: Setup programs can install modules in other
43-
directories, such as the `Program Files` directory. The installer may append
44-
the application location to the value of `$env:PSModulePath`.
24+
- Modules installed in the **CurrentUser** scope are stored in
25+
`$HOME\Documents\WindowsPowerShell\Modules`.
26+
- Modules installed in the **AllUsers** scope are stored in
27+
`$env:ProgramFiles\PowerShell\Modules`.
28+
- Modules that ship with Windows PowerShell stored in `$PSHOME\Modules`, which
29+
is `$env:SystemRoot\System32\WindowsPowerShell\1.0\Modules`.
4530

4631
## PowerShell PSModulePath construction
4732

4833
The value of `$env:PSModulePath` is constructed each time PowerShell starts.
49-
The value varies by version of PowerShell and how it's launched.
34+
The value varies by version of PowerShell and how you launched it.
5035

5136
### Windows PowerShell startup
5237

@@ -84,13 +69,13 @@ d---- 9/13/2019 3:53 PM 2.1.2
8469
By default, PowerShell loads the highest version number of a module when
8570
multiple versions are found. To load a specific version, use `Import-Module`
8671
with the **FullyQualifiedName** parameter. For more information, see
87-
[Import-Module][02].
72+
[Import-Module][03].
8873

8974
## Modifying PSModulePath
9075

9176
For most situations, you should be installing modules in the default module
92-
locations. However, you may have a need to change the value of the
93-
`PSModulePath` environment variable.
77+
locations. However, you might need to change the value of the `PSModulePath`
78+
environment variable.
9479

9580
For example, to temporarily add the `C:\Program Files\Fabrikam\Modules`
9681
directory to `$env:PSModulePath` for the current session, type:
@@ -102,35 +87,35 @@ $Env:PSModulePath = $Env:PSModulePath+";C:\Program Files\Fabrikam\Modules"
10287
To change the value of `PSModulePath` in every session, edit the registry key
10388
storing the `PSModulePath` values. The `PSModulePath` values are stored in the
10489
registry as _unexpanded_ strings. To avoid permanently saving the
105-
`PSModulePath` values as _expanded_ strings, use the **GetValue** method on the
90+
`PSModulePath` values as _expanded_ strings, use the `GetValue()` method on the
10691
subkey and edit the value directly.
10792

10893
The following example adds the `C:\Program Files\Fabrikam\Modules` path to the
10994
value of the `PSModulePath` environment variable without expanding the
110-
un-expanded strings.
95+
unexpanded strings.
11196

11297
```powershell
113-
$key = (Get-Item 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager').OpenSubKey('Environment', $true)
98+
$key = (Get-Item 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment')
11499
$path = $key.GetValue('PSModulePath','','DoNotExpandEnvironmentNames')
115100
$path += ';%ProgramFiles%\Fabrikam\Modules'
116101
$key.SetValue('PSModulePath',$path,[Microsoft.Win32.RegistryValueKind]::ExpandString)
117102
```
118103

119-
To add a path to the user setting, change the registry provider from `HKLM:\`
120-
to `HKCU:\`.
104+
To add a path to the user setting, use the following code:
121105

122106
```powershell
123-
$key = (Get-Item 'HKCU:\').OpenSubKey('Environment', $true)
107+
$key = (Get-Item 'HKCU:\Environment')
124108
$path = $key.GetValue('PSModulePath','','DoNotExpandEnvironmentNames')
125109
$path += ';%ProgramFiles%\Fabrikam\Modules'
126110
$key.SetValue('PSModulePath',$path,[Microsoft.Win32.RegistryValueKind]::ExpandString)
127111
```
128112

129113
## See also
130114

131-
- [about_Modules][03]
115+
- [about_Modules][01]
116+
- [about_Windows_PowerShell_Compatibility][02]
132117

133118
<!-- link references -->
134-
[01]: xref:PowerShellGet.Install-Module
135-
[02]: xref:Microsoft.PowerShell.Core.Import-Module
136-
[03]: about_Modules.md
119+
[01]: about_Modules.md
120+
[02]: /powershell/module/microsoft.powershell.core/about/about_windows_powershell_compatibility
121+
[03]: xref:Microsoft.PowerShell.Core.Import-Module

0 commit comments

Comments
 (0)