How do I debug a Windows PowerShell module in Visual Studio?

10,679

It's possible to debug your cmdlet directly without needing a separate project. Open the properties of your class library project and configure the Debug tab as follows (assuming Windows is installed to C:):

  • Start Action
    • Start external program: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
  • Start Options
    • Command line arguments: -NoLogo -Command "Import-Module '.\MyModule.dll'; Get-TestCommand;"

Class library project configuration for PowerShell debugging

Debugging a PowerShell cmdlet

Share:
10,679
DavidM
Author by

DavidM

Updated on June 13, 2022

Comments

  • DavidM
    DavidM almost 2 years

    I'm trying to write a PowerShell module as a VB.NET project with Visual Studio 2015. I've been able to put a few commands together, compile the class library into a DLL, import the module into a PowerShell session and call the command I created. All good so far. But as I'm expanding this module I'm going to need to be able to debug it.

    So I added another project to the solution, a console application. I set it as the startup project and referenced the PowerShell class in the first project. So far when I call the PowerShell function I wrote all the work is being done in the EndProcessing() subroutine. I can't call that from my console app because It's protected.

    The question: How do I properly call my Get-TestCommand PowerShell function from a console app in such a way that Visual Studio knows I'm referencing code in a separate project and not the compiled DLL while triggering the breakpoints I put in the Powershell class library?

  • rrirower
    rrirower about 8 years
    Please avoid one line answers with links. The links may go stale in the future.
  • Lance U. Matthews
    Lance U. Matthews about 8 years
    Please avoid wall-of-text answers from links. The reader's eyes may cross in the future.
  • Claudius
    Claudius about 8 years
    I'll leave first paragraph.
  • DavidM
    DavidM about 8 years
    I guess I wasn't clear. I'm not writing/debugging powershell code per-say. I'm writing VB.net code that get compiled into a DLL and that get's called from powershell. I'm stuck trying to debug the vb.net, I haven't gotten to the actual powershell coding to use the DLL. My understanding is PowerShell tools and PowerShell ISE are for Powershell code not the source DLLs they use.
  • Claudius
    Claudius about 8 years
    Check edit and are you already calling the dll from powershell script?
  • DavidM
    DavidM about 8 years
    That worked better than I'd hoped. I added a -NoExit to the Command line to keep the PowerShell window open after debugging to make things even easier. Thanks!
  • theta-fish
    theta-fish over 6 years
    In my experience, installing Powershell Tools for Visual Studio DISABLES the ability to debug .NET code when running from within powershell, whereas it works quite well without it if you manually attach the VS debugger to the powershell instance.
  • Stefan
    Stefan almost 6 years
    How can you debug code elevated to set Set-ExecutionPolicy RemoteSigned?