Debugging Powershell Modules in Visual Studio 2010


There are posts all over the web lamenting how difficult it is to debug a custom PowerShell Module from within Visual Studio 2010 but I found a simple way to do it that works every time.  It was tricky to get right at first, but this works like a champ.

Here are the steps:

  • Start Visual Studio
  • go the the Properties window for your custom Module Project
  • Select the ‘Start External Program’ radio button
  • enter “C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe” as the destination.  This will start PowerShell and attach the debugger to the instance

DebugPowerShellExternalProgram

  • Go down to the “Start Options” and enter the text below in the “Command Line Agruments” box, including all of the quotes (naturally, you will have to change the paths for your project):

-noexit -command “& { import-module -name ‘C:\Path\To\Solution\Project\bin\Debug\CustomModule.dll’}”

DebugPowerShellCommandLine

This last step loads your custom module into the PowerShell instance that is being debugged.  From there, simply set your breakpoints in Visual Studio and then call your new PowerShell command from within the new PowerShell window.  It should stop on your breakpoint every time.

Happy Debugging!

Just moved in!


Hey!  It’s me, I’m back, at least for a while.

I have been out of the blog world for a few years but am back now and hopefully will be able to kick out some decent posts.  They won’t be frequent but should be useful.

The old blog is still is still alive over on VSPug but I will fully move from there in a few weeks.  I am very grateful to the operator over at VSPUG who keeps that site running out of his own pocket!  Hopefully, getting my content off there will help him move a little closer to freeing up some pocket change 🙂

I’ve brought all of my original posts here just to keep everything together but please bear in mind that many of these posts relate to SharePoint 2007, and often even pre SP1.  If you hit something that just ain’t right, drop me a note and I’ll see if I can’t get it cleaned up.

Also, I am new to this blogging software so please bear with me while I figure out what all the knobs, buttons and levers do.  I know not to press the big red one, though.  Big shiny red buttons are always ‘bad’