How to Create a Powershell 2.0 Module and Cmdlet with Visual Studio 2010 (Screencast included)


This is one of those tasks I need to repeat every now and then and always forget the exact steps. So in the interest of sharing, I’ve written this post to demonstrate how to get started building a very simple module and cmdlet with C# and Visual Studio.

 

A SCREENCAST IS AVAILABLE

If you’d rather watch, this screencast will show you the steps I followed

image

https://vimeo.com/10007373

 

STEP-BY-STEP INSTRUCTIONS

Create a new Visual Studio Class Library Project project.

image

image

In this case, I named the library “DemoPS”.

Now, we will add two references. The first to System.Management and the second to System.Management.Automation

image

System.Management is found under the .NET tab

image

System.Management.Automation is found under c:\program files\reference assemblies\microsoft\WindowsPowershell\v1.0

image

image 

image

And now the references are available in Solution Explorer

image 

 

If you are using Visual Studio 2010 the assembly will be built using .NET 4.0, but you’ll need to switch it to use the older framework so that Powershell can load the assembly.

Edit the properties of the DemoPS project and set the Target Framework to “.NET Framework 3.5”

image

image

 

Now let’s the examine the code …

image

 

Build the cmdlet and launch Powershell 2.0

Import the module with Import-Module

image

The Get-Modules cmdlet will show you that the module is loaded and it has a single cmdlet inside it called “Get-DemoNames”

image

the “-?” parameter is already provided for the cmdlet

image

Now try the cmdlet …

image

And, of course, it behaves just like any other cmdlet. So you can do things like pipe its output …

image


Comments (7)

  1. Awesome article. I have tried it on windows 8.1 with VS 2012. Result is awesome. 🙂

  2. Martin Jæger says:

    Great way to get going. It's an awesome icebreaker.

    Helped me.

    Thanks!

  3. Thomas Tang says:

    Helped me. Thanks

  4. Thanks, but says:

    Don't display code as an image please.

  5. Steve Friedl says:

    This was very helpful, thank you.

    I did need to install the Windows PowerShell 2.0 SDK to get the proper Systems.Management.Automation reference.

    Found at: http://www.microsoft.com/…/confirmation.aspx

  6. andrew morgan says:

    for anyone else who wants to just copy > paste:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace some.namespace

    {

       [System.Management.Automation.Cmdlet(System.Management.Automation.VerbsCommon.Get,"DemoNames")]

       public class Get_DemoNames: System.Management.Automation.PSCmdlet

       {

           [System.Management.Automation.Parameter(Position = 0, Mandatory = false)]

           public string prefix;

           protected override void ProcessRecord()

           {

               var names = new string[] {"Chris","Charlie","Isaac","Simon"};

               if (string.IsNullOrEmpty(this.prefix))

               {

                   this.WriteObject(names, true);

               }

               else

               {

                   var prefixed_names = names.Select(n => this.prefix + n);

                   this.WriteObject(prefixed_names, true);

               }

           }

       }

    }

  7. MichaelBlankenship.ftw says:

    What would be nice would be a follow-up article which explains how to deploy this, say, under that C:WindowsSystem32WindowsPowershellv1.0Modules subdirectory structure.  You'd then think that it would have the following:

    ..ModulesNameOfModuleen-USsomething.xml

    ..ModulesNameOfModuleNameOfModule.psd1

    …and then in theory, you wouldn't need to implicitly import the module since it would be available when you fire up PowerShell.