Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create powershell 2.0 modules?

Tags:

powershell

I've heard powershell 2.0 CTP has modules, but I can't find much example code or instructions. I've read what little help there seems to be online...

But I just keep getting "The term 'Add-Module' is not recognized as a cmdlet..." when I try and load a module.

Any help would be gratefully received!

Edit (July 2010) Please note this question is based on powershell 2.0 CTP and is therefore a year and half out of date! Please see Samuel Jack's answer for help with the powershell 2.0 RTM.

like image 286
Chris Haines Avatar asked Dec 04 '08 17:12

Chris Haines


People also ask

How do I install a PowerShell v2 module?

To install the module for the first time, complete the following steps: Install or update the PowerShellGet module as described in Installing PowerShellGet. Close and re-open the Windows PowerShell window. Now you can use the Install-Module cmdlet to install the module from the PowerShell Gallery.

Can you create a GPO PowerShell?

Notes. Only domain administrators, enterprise administrators, and members of the Group Policy creator owners group can create GPOs. These users must run Windows PowerShell in an elevated state. You can use the Domain parameter to explicitly specify the domain for this cmdlet.


2 Answers

With the Win7 build, Add-Module is gone. The new cmdlet is Import-Module. The easiest way to create a module is rename a PS1 file to a PSM1 file. From there you can do all sorts of things including the module manifest.

like image 142
Andy Schneider Avatar answered Oct 04 '22 16:10

Andy Schneider


I'm no Powershell expert, but here's what I just figured out using PowerShell 2.0 RTM.

Suppose you want to create a module called MyModule:

  1. Make sure that you have created the folder %My Documents%\WindowsPowershell\Modules
  2. Create a folder inside Modules called MyModule
  3. Put your code in a file inside MyModule and name the file MyModule.psm1
  4. Remember to use the Export-ModuleMember command as the last thing in your script file. Export-ModuleMember -Function * -Alias * will export all functions and aliases
  5. In scripts where you want to use the module, use the command Import-Module MyModule

By default Powershell is configured not to run any kinds of scripts from files, so you need to alter the security settings. Set-ExecutionPolicy Unrestricted will get you going if you're not concerned about scripts needing to be signed.

like image 31
Samuel Jack Avatar answered Oct 04 '22 17:10

Samuel Jack