PowerShell using netcoreapp3.0 assembly: Add-Type fails with "Could not load type 'System.Object' from assembly 'System.Private.CoreLib"

Viewed 652

I need to use one of my dotnet core 3 assemblies within a powershell script:

try { 
    Add-Type -Path "app.dll" 
}
catch { 
    $_.Exception.LoaderExceptions 
}

This fails:

Could not load type 'System.Object' from assembly 'System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' because the parent does 
not exist.

Why I try to Add-Type System.Private.CoreLib.lib, the same error occurs. It seems to work well with a netstandard2.0 assembly, but not with my netcoreapp3.0 assembly. What can I do in order to use the methods of my assembly in the powershell script?

1 Answers

If you need to load in PowerShell .NET assemblies that are targeting above netstandard2.0 version, then you should try to execute your script in PowerShell Core 7.1.3 or above.

Windows PowerShell might be able to load only .NET assemblies that are targeting .NET Framework or netstandard2.0.

Related