Welcome to MSDN Blogs Sign in | Join | Help

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 http://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 http://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.

Published Sunday, March 04, 2007 3:53 PM by jmstall
Filed under:

Comments

# re: Building and Debugging Powershell cmdlets in the VS IDE

Monday, March 05, 2007 5:06 AM by nikhilbhandari

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.

# re: Building and Debugging Powershell cmdlets in the VS IDE

Monday, March 05, 2007 10:14 AM by jmstall

# re: Building and Debugging Powershell cmdlets in the VS IDE

Wednesday, April 25, 2007 8:51 PM by colinn

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?

# re: Building and Debugging Powershell cmdlets in the VS IDE

Wednesday, April 25, 2007 10:59 PM by jmstall

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

# BDD 2007 & PowerShell : comment faire une cmdlet ?

Friday, May 18, 2007 5:39 AM by Blog de David Sebban [MSFT]

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

# Developing Live Writer PlugIns with Edit-and-Continue

Wednesday, October 24, 2007 1:25 AM by Mike Stall's .NET Debugging Blog

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

# Using Windows live Writer

Saturday, October 27, 2007 12:21 AM by Mike Stall's .NET Debugging Blog

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

# Twitter PowerShell Script

Monday, November 10, 2008 5:46 AM by Mike Ormond's Blog

Here's the PowerShell script I came up with that I referred to in my previous post that integrates with

# PowerShell and BizTalk RFID (1) - Providers and Server Configuration

Tuesday, March 17, 2009 5:48 PM by Rfid Factotum

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New",

New Comments to this post are disabled
 
Page view tracker