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:
Set a Breakpoint in the method you would like to debug.
Build the project.
Start a new PowerShell window.
In Visual Studio, select “Attach to Process” from the Debug menu.
Select the PowerShell.exe process and click Attach. The debugger has now started as is attached to the PowerShell window you have opened.
In the PowerShell window, import your module and run your PowerShell Cmdlet.
When your code hits the breakpoint it will let you step through it in the Visual Studio debugger like any other Visual Studio solution.