[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!

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Pin It on Pinterest

Share This