Q: How can I convert a PowerShell script into a module that works like a standard cmdlet?

How to convert a PowerShell script to a module.

John Savill

January 2, 2013

1 Min Read
Q: How can I convert a PowerShell script into a module that works like a standard cmdlet?

A: Typically, Windows PowerShell scripts are saved as ps1 files. However, if a file is saved as a psm1 file, it can be treated as a module.

You need to ensure your script has functions defined in it that will become the cmdlet names used. For example,

function Wakeup-Machine
{
all your existing code}

The psm1 file must be saved in a location that's searched when PowerShell starts and that can be viewed and changed via the PSModulePath environment variable.

Additionally when a PowerShell instance starts, your My DocumentsWindowsPowerShellModules folder is automatically added to the path.

This means if you create a subfolder WindowsPowerShellModules within your My Documents folder, you can save your psm1 files to that folder, and they will automatically be available via a 'get-modules -listavailable' and then loaded as required using import-module. To learn more about PowerShell, see our PowerShell FAQs. You might also want to read  "Top 10 Active Directory Tasks Solved with PowerShell."

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like