In the context of Cmdlet development, and having issues with unloading your DLL, there are two approaches that I use.
Firstly, I develop in Visual Studio, and setup an external program (PowerShell) to load my Cmdlet. This way, my module loads when I start debugging, and unloads when I stop debugging.
Second, on those occasions where I know I want to load a module, do some work, and ensure the module is unloaded afterwards, I use a second instance of PowerShell. This has been discussed in other answers, and my answer below shows how I enable this workflow by using a function with an alias in my Profile. I change the prompt so I can have a visual reminder that I am in a "recursive PowerShell window".
Create a script in your profile to start PowerShell
function Start-DebugPowerShell
{
PowerShell -NoProfile -NoExit -Command {
function prompt {
$newPrompt = "$pwd.Path [DEBUG]"
Write-Host -NoNewline -ForegroundColor Yellow $newPrompt
return '> '
}
}
}
Set-Alias -Name sdp -Value Start-DebugPowerShell
Edit debug settings for your Cmdlet project
Start external program:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Command line arguments:
-NoProfile -NoExit -Command "Import-Module .\MyCoolCmdlet.dll"
Debug your Module
Now from Visual Studio, start debugger with F5, and you have a new PowerShell window with your Cmdlet loaded, and you can debug it however you like.
Use the 'sdp' alias from any PowerShell window
Since the Start-DebugPowerShell function is in our profile and we gave it an alias sdp
, you can use this to start a second instance of PowerShell anytime you need it.