Powershell Core - loading assemblies

Not applicable

I'm currently engaged in updating Powershell Code to work with Powershell Core (I expect a lot of us are). I've been able to overcome all obstacles so far, except this one!


The existing script loads the assemblies System.Windows.Forms and System.Windows.Forms.DataVisualization (both using [Reflection.Assembly]::LoadWithPartialName()) and creates a System.Windows.Forms.DataVisualization.Charting.Chart object. The same code, run without changes in Powershell core (and on the same machine), fails.


System.Windows.Forms fails to load with:
Exception calling "LoadWithPartialName" with "1" argument(s): "Could not load file or assembly 'System.Windows.Forms, Culture=neutral, PublicKeyToken=null'. Operation is not supported. (Exception from HRESULT: 0x80131515)"

and System.Windows.Forms.DataVisualization appears to load (i.e doesn't throw an exception) BUT the console display starts like this:
GAC   Version
---      -------
False   v4.0.30319

and a subsequent call to create a charting object fails with:
Cannot find type [System.Windows.Forms.DataVisualization.Charting.Chart]: verify that the assembly containing this type is loaded.


I have read that the [Reflection.Assembly] methods of loading .Net assemblies are deprecated and have tried following the guidelines to use Add-Type instead. These also fail in very similar ways i.e:
System.Windows.Forms - fails to load with an exception
System.Windows.Forms.DataVisualization appears to load but does not export any types

The methods I have tried so far are:
Add-Type -AssemblyName (with partial & strong names)
Add-Type -Path

In every case, the code works fine on PS5 and fails in Powershell Core.

Are these assemblies simply no longer supported in Powershell Core?


3 Replies

PowerShell Core is not a "replacement" or "upgrade" for "regular" PowerShell, it's designed to solve different scenarios. None of which have understanding of "UI", apart from the console. Now, afaik the .NET Core folks are actually working on some sort of "port" for the Forms library, but I wouldn't hold my breath (see the announcement here: https://devblogs.microsoft.com/dotnet/net-core-3-and-support-for-windows-desktop-applications/).


In the meantime, for anything that requires even a simple UI-based prompt, keep using good old Windows PowerShell.


@Vasil Michev 


If PowerShell Core is not a replacement for Windows PowerShell, then why is there no longer any ongoing development or maintenance on Windows PowerShell, with 100% of the PowerShell team working exclusively on PowerShell Core?


That's a question you should be asking the relevant folks at Microsoft. It was a poor judgement on their end, one that will finally be addressed with the release of v7. You can read the announcement blog for more details: https://devblogs.microsoft.com/powershell/the-next-release-of-powershell-powershell-7/