PowerShell Does Not Reload Upgraded Assemblies

September 6 2010 4 comments

If you use PowerShell while developing with SharePoint 2010, you may have come across this. After you upgrade your packages, PowerShell uses old versions of the assemblies that were supposed to be upgraded. This is because the assemblies are loaded to PowerShell’s app domain, and as long as they are used from the same app domain, they do not get reloaded. I guess the problem does not exist if you manipulate SharePoint with stsadm, but SharePoint PowerShell Commandlets seem to command SharePoint within the same PowerShell app domain.

The problem has a very simple solution: just close and reopen the PowerShell window. But after you have done that a couple of hundred times, you will probably get… erm… annoyed. I want that every time I invoke my script, it starts in a fresh app domain. That’s why my script starts as follows:

param(
<other script parameters>
[switch]$thisAppDomain
)

if(-not $thisAppDomain)
{
    Write-Host "Invoking script in a new app domain"  -foregroundcolor yellow
    powershell.exe -Command $MyInvocation.Line -thisAppDomain
    return;
}

The code starts a new PowerShell process (and a new app domain) with the same arguments as the current invocation, it just adds one extra argument, -thisAppDomain. I know it’s not bullet proof and fails if your script invocation includes piping, for example. Still, it works most of the time and I am not required to reopen the PowerShell window any more to get the dll’s reloaded. If anyone knows a better way to accomplish the “refreshing” of app domain, please share!

Popularity: 3% [?]

4 comments to “PowerShell Does Not Reload Upgraded Assemblies”

  1. Yury says:

    Thanks.
    But aren`t there any way for unloading assemblies?
    Sometimes i write somthing in powershell ISE and use Add-Type cmdlet for add some c# code.
    ISE is too havy for often restarts.

    Sorry for my english.

  2. magnificent issues altogether, you simply received a new reader.
    What might you recommend about your put up that you
    made some days ago? Any sure?

  3. GAMETIPS says:

    Wow, beautiful site. Thnx ..

  4. thanks for clarifying it :)

Leave a Reply