11---
22description : How to Write a PowerShell Binary Module
3- ms.date : 09/13/2016
3+ ms.date : 06/12/2025
44title : How to Write a PowerShell Binary Module
55---
66# How to Write a PowerShell Binary Module
77
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.
1414
1515The following procedure describes how to create and install a PowerShell binary module.
1616
@@ -19,10 +19,10 @@ The following procedure describes how to create and install a PowerShell binary
19191 . Create a binary PowerShell solution (such as a cmdlet written in C#), with the capabilities you
2020 need, and ensure that it runs properly.
2121
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 ] .
2626
27271 . If necessary, create the rest of your solution: (additional cmdlets, XML files, and so on) and
2828 describe them with a module manifest.
@@ -32,9 +32,10 @@ The following procedure describes how to create and install a PowerShell binary
3232 files will go into the module. As stated previously however, PowerShell can treat a binary cmdlet
3333 like a module with no additional effort. As such, a module manifest is useful mainly for
3434 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 ] .
3636
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.
3839
3940 ``` csharp
4041 using System .Management .Automation ; // Windows PowerShell namespace.
@@ -73,37 +74,58 @@ The following procedure describes how to create and install a PowerShell binary
7374
74751 . Package your solution, and save the package to somewhere in the PowerShell module path.
7576
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.
8283
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
8586 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 ] .
8889
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 ] .
9091
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 ] .
9495
95- ## Importing Snap-in Assemblies as Modules
96+ ## Module initialization and cleanup code
97+
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.
101+
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
96107
97108Cmdlets and providers that exist in snap-in assemblies can be loaded as binary modules. When the
98109snap-in assemblies are loaded as binary modules, the cmdlets and providers in the snap-in are
99- available to the user, but the snap-in class in the assembly is ignored, and the snap-in is not
100- 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
101112snap-in even though the cmdlets and providers are available to the session.
102113
103- 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
104115part of a binary module. To import the formatting and types files you must create a module manifest.
105- 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 ] .
106117
107118## See Also
108119
109- [ Writing a Windows PowerShell Module] ( ./writing-a-windows-powershell-module.md )
120+ - [ Writing a Windows PowerShell Module] [ 06 ]
121+
122+ <!-- link references -->
123+ [ 01 ] : ../cmdlet/writing-a-windows-powershell-cmdlet.md
124+ [ 02 ] : /powershell/module/microsoft.powershell.core/about/about_psmodulepath
125+ [ 03 ] : how-to-write-a-powershell-module-manifest.md
126+ [ 04 ] : importing-a-powershell-module.md
127+ [ 05 ] : installing-a-powershell-module.md
128+ [ 06 ] : writing-a-windows-powershell-module.md
129+ [ 07 ] : xref:Microsoft.PowerShell.Core.Import-Module
130+ [ 08 ] : xref:System.Management.Automation.IModuleAssemblyCleanup
131+ [ 09 ] : xref:System.Management.Automation.IModuleAssemblyInitializer
0 commit comments