How do I debug a Windows PowerShell module in Visual Studio?
It's possible to debug your cmdlet directly without needing a separate project.
Note that the .exe
paths below are for the "native" application versions — 32-bit (Windows) Powershell on 32-bit Windows, 64-bit (Windows) Powershell on 64-bit Windows — and assume Windows is installed to C:
.
Project configuration
For .NET (Core) projects in Visual Studio 2022
Open the properties of your class library project.
Under the
Debug
section, click theOpen debug launch profiles UI
link.In the
Launch Profiles
window, clickCreate a new profile
(first button in the upper-left) and selectExecutable
.Configure the new launch profile as follows:
Executable
:C:\Program Files\PowerShell\7\pwsh.exe
Command line arguments
:-NoLogo -Command "Import-Module '.\MyModule.dll'; Get-TestCommand;"
Close the
Launch Profiles
window.Click the downward-pointing arrow in the
Start Debugging
toolbar button and select the new launch profile.
For .NET Framework projects in Visual Studio 2022, or all .NET projects in Visual Studio 2019 and older
Open the properties of your class library project and configure the Debug
tab as follows:
Start Action
Start external program:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
orC:\Program Files\PowerShell\7\pwsh.exe
Start Options
Command line arguments:
-NoLogo -Command "Import-Module '.\MyModule.dll'; Get-TestCommand;"