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<br>
{<br>
all your existing code}<br><br>

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 Documents\WindowsPowerShell\Modules folder is automatically added to the path.

This means if you create a subfolder WindowsPowerShell\Modules 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."

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottYou'll have the opportunity to experience:
• The Microsoft
Technology Roadmap
• Office 365 Implementation
• Hyper-V Optimizing
• Windows 8 Deployment
and much more!

Come See Paul Thurrott & Rod Trent in Person!

Early Registration Now Open

Upcoming Training

Mastering System Center 2012

During over 6 hours of training you can join John Savill from your computer as he will walk you through the key components and capabilities of System Center 2012, what’s involved in using the components, and the benefit they can bring to your environment.

Register Now

Current Issue

May 2013 - The NameTranslate object is useful when you need to translate Active Directory object names between different formats, but it's awkward to use from PowerShell. Here's a PowerShell script that eliminates the awkwardness.

CURRENT ISSUE / ARCHIVE / SUBSCRIBE

Windows Forums

Get answers to questions, share tips, and engage with the Windows Community in our Forums.