How to install PowerShell module from .NET application

Volodymyr Dumchykov 56 Reputation points
2023-12-01T16:46:28.6233333+00:00

Hi

I need to install a PowerShell module from my .NET application. I have added Microsoft.PowerShell.SDK nuget package to the project and use PowerShell class to execute Install-Module cmdlet. Unfortunatly the execution failed with the following error:

No match was found for the specified search criteria and provider name 'NuGet'. Try 'Get-PackageProvider -ListAvailable' to see if the provider exists on the system.

As I understood, Microsoft.PowerShell.SDK includes neither PackageManagement nor PowerShellGet module which are required for Install-Module cmdlet.

I'v also tries to install nuget package provider via Install-PackageProvider cmdlet. It failed with the following exception

Unhandled Exception - Message:'The type initializer for 'Microsoft.PackageManagement.Internal.Utility.Plugin.DynamicType' threw an exception.' Name:'TypeInitializationException' Stack Trace:'

Any idias what I do wrong?

Windows for business Windows Server User experience PowerShell
Developer technologies .NET Other
{count} vote

1 answer

Sort by: Most helpful
  1. Jens Schmitz 5 Reputation points
    2023-12-04T22:09:09.07+00:00

    Hi @Volodymyr Dumchykov

    Couldn't you just start a new PowerShell process and then pass the Install-Module as an argument?

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.