You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: How to Write a PowerShell Binary Module
3
-
ms.date: 09/13/2016
3
+
ms.date: 06/12/2025
4
4
title: How to Write a PowerShell Binary Module
5
5
---
6
6
# How to Write a PowerShell Binary Module
7
7
8
-
A binary module can be any assembly (.dll) that contains cmdlet classes. By default, all the cmdlets
9
-
in the assembly are imported when the binary module is imported. However, you can restrict the
10
-
cmdlets that are imported by creating a module manifest whose root module is the assembly. (For
11
-
example, the CmdletsToExport key of the manifest can be used to export only those cmdlets that are
12
-
needed.) In addition, a binary module can contain additional files, a directory structure, and other
13
-
pieces of useful management information that a single cmdlet cannot.
8
+
A binary module can be any assembly (`.dll`) that contains cmdlet classes. By default, all the
9
+
cmdlets in the assembly are imported when the binary module is imported. However, you can restrict
10
+
the cmdlets that are imported by creating a module manifest whose root module is the assembly. (For
11
+
example, the **CmdletsToExport** key of the manifest can be used to export only those cmdlets that
12
+
are needed.) In addition, a binary module can contain additional files, a directory structure, and
13
+
other pieces of useful management information that a single cmdlet cannot.
14
14
15
15
The following procedure describes how to create and install a PowerShell binary module.
16
16
@@ -19,10 +19,10 @@ The following procedure describes how to create and install a PowerShell binary
19
19
1. Create a binary PowerShell solution (such as a cmdlet written in C#), with the capabilities you
20
20
need, and ensure that it runs properly.
21
21
22
-
From a code perspective, the core of a binary module is simply a cmdlet assembly. In fact,
23
-
PowerShell will treat a single cmdlet assembly as a module, in terms of loading and unloading,
24
-
with no additional effort on the part of the developer. For more information about writing a
25
-
cmdlet, see [Writing a Windows PowerShell Cmdlet](../cmdlet/writing-a-windows-powershell-cmdlet.md).
22
+
From a code perspective, the core of a binary module is a cmdlet assembly. In fact, PowerShell
23
+
treats a single cmdlet assembly as a module for loading and unloading, with no additional effort
24
+
on the part of the developer. For more information about writing a cmdlet, see
25
+
[Writing a Windows PowerShell Cmdlet][01].
26
26
27
27
1. If necessary, create the rest of your solution: (additional cmdlets, XML files, and so on) and
28
28
describe them with a module manifest.
@@ -32,9 +32,10 @@ The following procedure describes how to create and install a PowerShell binary
32
32
files will go into the module. As stated previously however, PowerShell can treat a binary cmdlet
33
33
like a module with no additional effort. As such, a module manifest is useful mainly for
34
34
combining multiple files into a single package, or for explicitly controlling publication for a
35
-
given assembly. For more information, see [How to Write a PowerShell Module Manifest](how-to-write-a-powershell-module-manifest.md).
35
+
given assembly. For more information, see [How to Write a PowerShell Module Manifest][03].
36
36
37
-
The following code is an extremely simple C# code block that contains three cmdlets in the same file that can be used as a module.
37
+
The following code is a simplified C# example that contains three cmdlets in the same file that
38
+
can be used as a module.
38
39
39
40
```csharp
40
41
usingSystem.Management.Automation; // Windows PowerShell namespace.
@@ -73,41 +74,58 @@ The following procedure describes how to create and install a PowerShell binary
73
74
74
75
1. Package your solution, and save the package to somewhere in the PowerShell module path.
75
76
76
-
The `PSModulePath` global environment variable describes the default paths that PowerShell will
77
-
use to locate your module. For example, a common path to save a module on a system would be
78
-
`%SystemRoot%\Users\<user>\Documents\WindowsPowerShell\Modules\<moduleName>`. If you do not use
79
-
the default paths, you will need to explicitly state the location of your module during
80
-
installation. Be sure to create a folder to save your module in, as you may need the folder to
81
-
store multiple assemblies and files for your solution.
77
+
The `$env:PSModulePath` global environment variable describes the default paths that PowerShell
78
+
uses to locate your module. For example, a common path to save a module on a system would be
79
+
`%SystemRoot%\Users\<user>\Documents\WindowsPowerShell\Modules\<moduleName>`. If you don't use
80
+
the default paths, you need to explicitly state the location of your module during installation.
81
+
Be sure to create a folder to save your module in, as you may need the folder to store multiple
82
+
assemblies and files for your solution.
82
83
83
-
Note that technically you do not need to install your module anywhere on the `PSModulePath` -
84
-
those are simply the default locations that PowerShell will look for your module. However, it is
84
+
Technically, you don't need to install your module anywhere on the `$env:PSModulePath` - those
85
+
are simply the default locations that PowerShell will look for your module. However, it's
85
86
considered best practice to do so, unless you have a good reason for storing your module
86
-
somewhere else. For more information, see [Installing a PowerShell Module](./installing-a-powershell-module.md)
87
-
and [about_PSModulePath](/powershell/module/microsoft.powershell.core/about/about_psmodulepath).
87
+
somewhere else. For more information, see [Installing a PowerShell Module][05] and
88
+
[about_PSModulePath][02].
88
89
89
-
4. Import your module into PowerShell with a call to [Import-Module](/powershell/module/Microsoft.PowerShell.Core/Import-Module).
90
+
1. Import your module into PowerShell with a call to [Import-Module][07].
90
91
91
-
Calling to [Import-Module](/powershell/module/Microsoft.PowerShell.Core/Import-Module) will load
92
-
your module into active memory. If you are using PowerShell 3.0 and later, simply calling the
93
-
name of your module in code will also import it; for more information, see [Importing a PowerShell Module](./importing-a-powershell-module.md).
92
+
Calling to [Import-Module][07] loads your module into active memory. If you are using PowerShell
93
+
3.0 and later, invoking a command from your module in code also imports it. For more information,
94
+
see [Importing a PowerShell Module][04].
94
95
95
-
## Module Initialization and Cleanup Code
96
+
## Module initialization and cleanup code
96
97
97
-
If your module needs to do something upon import or removal such as a discovery task or initialization, you can implement the `IModuleAssemblyInitializer` and `IModuleAssemblyCleanup` interfaces. Please note this is discouraged unless absolutely necessary, as to keep PowerShell performant you should lazily load things at the point your cmdlet(s) are called rather than on import.
98
+
If your module needs to do something upon import or removal such as a discovery task or
99
+
initialization, you can implement the [`IModuleAssemblyInitializer`][09] and
100
+
[`IModuleAssemblyCleanup`][08] interfaces.
98
101
99
-
## Importing Snap-in Assemblies as Modules
102
+
> [!NOTE]
103
+
> This pattern is discouraged unless absolutely necessary. To keep PowerShell performant, you should
104
+
> lazily load things at the point your commands are called rather than on import.
105
+
106
+
## Importing snap-in assemblies as modules
100
107
101
108
Cmdlets and providers that exist in snap-in assemblies can be loaded as binary modules. When the
102
109
snap-in assemblies are loaded as binary modules, the cmdlets and providers in the snap-in are
103
-
available to the user, but the snap-in class in the assembly is ignored, and the snap-in is not
104
-
registered. As a result, the snap-in cmdlets provided by Windows PowerShell cannot detect the
110
+
available to the user, but the snap-in class in the assembly is ignored, and the snap-in isn't
111
+
registered. As a result, the snap-in cmdlets provided by Windows PowerShell can't detect the
105
112
snap-in even though the cmdlets and providers are available to the session.
106
113
107
-
In addition, any formatting or types files that are referenced by the snap-in cannot be imported as
114
+
In addition, any formatting or types files that are referenced by the snap-in can't be imported as
108
115
part of a binary module. To import the formatting and types files you must create a module manifest.
109
-
See, [How to Write a PowerShell Module Manifest](how-to-write-a-powershell-module-manifest.md).
116
+
See, [How to Write a PowerShell Module Manifest][03].
110
117
111
118
## See Also
112
119
113
-
[Writing a Windows PowerShell Module](./writing-a-windows-powershell-module.md)
0 commit comments