I am writing a PowerShell Cmdlet in C# using Visual Studio 2015. Debugging it works fine, but I cannot rebuild the DLL until I close the PowerShell window. The DLL seems to be in use and therefore cannot be deleted (not on the command line nor using explorer). I tried remove-module. This successfully removes my Cmdlet but I still cannot delete / overwrite it.
It is very unhandy to close PowerShell, rebuild the DLL and then reopen a new PowerShell, cd to the DLL path (usually deeply nested), re-import it again, start the command to debug, and so on for every single debugging session...
Is there no better solution to unload the DLL?
Anytime I see something like this:
It is very unhandy to close the powershell, rebuild the dll and then reopen a new powershell, cd to the dll path (usually deeply nested), re-import it again, start the command to debut, and so on for every single debugging session...
I immediately think, "I should create a script to do these tasks for me".
There are solutions. You can start a second PowerShell (like another answer suggested). Another solution is to use a script to do some work for you, and to top it off, you can add this to your VS project.
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.
I'm afraid there is not much you can do about this behavior as far as I know. One trick is to immediately start a new PowerShell session inside your existing session before loading the DLL. Then you can exit out of the second one and you have a brand new without the DLL loaded. Just remember to start a new "secondary" session before loading it again in case you need to unload it again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With