[PowerShell] How To Debug a Custom C# PowerShell Cmdlet

In my previous couple of posts I have been discussing how to write a custom PowerShell Cmdlet in C# ([PowerShell] Write a Custom PowerShell Cmdlet in C# and [PowerShell] Using Common Verbs for Custom PowerShell Cmdlets in C#). When developing custom PowerShell Cmdlets in C# you will probably come across the need to debug them in Visual Studio.

Unlike a console application, you can’t just hit debug and go. However, starting the debug process for a PowerShell Cmdlet is not difficult:

  1. Set a Breakpoint in the method you would like to debug.

  2. Build the project.

  3. Start a new PowerShell window.

  4. In Visual Studio, select “Attach to Process” from the Debug menu.

  5. Select the PowerShell.exe process and click Attach. The debugger has now started as is attached to the PowerShell window you have opened.

  6. In the PowerShell window, import your module and run your PowerShell Cmdlet.

  7. When your code hits the breakpoint it will let you step through it in the Visual Studio debugger like any other Visual Studio solution.

Happy Debugging!

John Chapman

Hello, I'm John Chapman. I am a SharePoint Developer for Sitrion (formerly NewsGator) living in Denver, Colorado. I develop solutions using SharePoint and .NET, and I thrive on the challenge of writing code to overcome the impossible, annoying, or otherwise difficult obstacles.

More Posts - Website - Twitter - LinkedIn - Google Plus