Share via


Building and Debugging Powershell cmdlets in the VS IDE

Here's how you can get a sweet Visual Studio development experience for building and debugging your own PowerShell cmdlet:
- It has Wizard support for initially creating the cmdlet,
- intellisense
- F5 build support which also registers your cmdlet
- provides the full power of the VS debugger including Edit-and-Continue when debugging your cmdlets.

David Aikens has a great tutorial for how to build a Power Shell cmdlet in the VS ide. This does 99% of the work. He's got great wizards that makes things very easy to start. His tutorial describes the rest of the steps that need to be done manually. Conveniently, all of these steps can be automated. I'm assuming there's already some sample VS project in the Power Shell SDK or somewhere that does that; but if you want to create such a project by hand, here's how...

  1. Install David's wizard from https://channel9.msdn.com/Photos/ZippedFiles/256835_PSTemplates.zip.  This provides a great integrated wizard in 'New Project' / 'Add New Item'  which has a starting implementation of a cmdlet.
     

  2. Add the installutil.exe command as a post-build step:
        c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\installutil.exe $(TargetDir)$(TargetFileName)
    This will register the snapin in a global list in the registry such that PowerShell can find it. After this command, the snap-in should show up with the 'get-pssnapin -registered' command in powershell.
     

  3. In the debug settings under Project Properties, for "Start Action", use "Start External Program", and specify Power shell:
        Eg, something like:
        C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe
    The Start Action controls what happens when you press 'F5'. If you're building an exe, usually you want it to just launch that exe. In our case, we're building a dll and we want it to PowerShell and have that load our dll.
     

  4. For command line arguments, specify:
        -noexit -command add-pssnapin test

    Where 'test' is the name of your snap-in that you registered (PSSnapIn.Name property in PsSnapin.cs). These specify the command line arguments to the exe we're launching. In our case, we're launching PowerShell, and we want PowerShell to automatically load our cmdlet. The -command parameter to PowerShell tells it to execute the 'add-pssnapin test' command at startup, which loads the cmdlet that you're building in the IDE. The -noexit switch tells PowerShell to not exit after doing -command.

    The Debug settings should look like:

     

  5. Try it out!  Also see https://msdn2.microsoft.com/en-us/library/ms714598.aspx  for more tips on writing a cmdlet, such as adding parameters.
     

Now you can develop your cmdlet in the IDE as you'd expect, complete with intellisense. When you Build in the IDE, it will automatically run installutil and install your cmdlet. When you F5, it will launch PowerShell under the debugger, and load your snap-in. Thus you can use the full power of the Visual Studio debugger on your cmdlet. When you execute your cmdlet, you'll hit breakpoints, etc.

You can even use edit-and-continue on your cmdlet.   (Unfortunately, EnC doesn't support adding public properties, so you can't add new properties to your cmdlet).

You can also enable Just-my-code which will treat your cmdlet as user-code and Power Shell as non-user code and make it easier to focus on debugging just your cmdlet without debugging Power Shell.

Comments

  • Anonymous
    March 04, 2007
    I am getting a error message while installing Windows PowerShell (CS).vsi. The error message is "String Cannot have zero length" I am using Visual Studio 2005.

  • Anonymous
    March 05, 2007
    N - it just worked for me. I'd ping David at http://blogs.msdn.com/daiken/archive/2007/02/07/creating-a-windows-powershell-cmdlet-using-the-visual-studio-windows-powershell-templates.aspx about that.

  • Anonymous
    April 25, 2007
    Now that I've set this up I am having a hard time understanding how to pass something to my cmdlet from the pipeline while in the debugger?

  • Anonymous
    April 25, 2007
    Colin - I haven't tried that, but you may be able to func-eval to the Write functions?

  • Anonymous
    May 17, 2007
    Comme Michael Niehaus l'expliquait il y a quelques temps, il est assez simple d'utiliser la DLL ConfigManager

  • Anonymous
    October 23, 2007
    I was playing around with Live Writer's (WLW) PlugIn API and am really impressed. It's clean, hassle-free,

  • Anonymous
    October 26, 2007
    I'm trying out Windows Live Writer. Currently, I do all of my blogging via Frontpage , so this will be

  • Anonymous
    November 09, 2008
    Here's the PowerShell script I came up with that I referred to in my previous post that integrates with

  • Anonymous
    March 17, 2009
    .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New",