How do I debug a Windows PowerShell module in Visual Studio?
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 external program:
- Start Options
- Command line arguments:
-NoLogo -Command "Import-Module '.\MyModule.dll'; Get-TestCommand;"
- Command line arguments:
DavidM
Updated on June 13, 2022Comments
-
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 about 8 yearsPlease avoid one line answers with links. The links may go stale in the future.
-
Lance U. Matthews about 8 yearsPlease avoid wall-of-text answers from links. The reader's eyes may cross in the future.
-
Claudius about 8 yearsI'll leave first paragraph.
-
DavidM about 8 yearsI 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 about 8 yearsCheck edit and are you already calling the dll from powershell script?
-
DavidM about 8 yearsThat 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 over 6 yearsIn 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 almost 6 yearsHow can you debug code elevated to set Set-ExecutionPolicy RemoteSigned?