Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto install a PowerShell binary cmdlet (C#) from a NuGet package with its dependencies

I've developed a binary cmdlet using C#. The project's TargetFramework is netstandard2.0 and the assembly (the dll) is published as a NuGet package. This assembly and thus the NuGet package has a few dependencies (i.e. other NuGets).

What is the best way to install my binary cmdlet locally on a computer using PowerShell, so my cmdlet becomes available on that computer to be called from PowerShell scripts. I.e., can I install the NuGet with a Powershell command such that my DLL and its dependencies become available to PowerShell?

like image 851
KarloX Avatar asked Oct 21 '25 04:10

KarloX


1 Answers

As of PowerShell 7.2, there is no support for direct installation of NuGet packages.

  • GitHub issue #6724 asks for such support.

Your options are:

  • If feasible, repackage your binary cmdlet and its dependencies as a PowerShell module, which can then be installed via Import-Module:

    • A PowerShell module is typically a directory named for the module, which contains a module manifest file (*.psd1) with metadata about the module alongside the files that make up the implementation of the module; in the case of a module wrapping a cmdlet DLL, the manifest's RootModule entry references that DLL, and the RequiredAssemblies entry references the dependencies.
    • When Publish-Module is called to publish a module, it is packaged as a NuGet package with a custom structure that Import-Module understands; typically, PowerShell modules are published to the PowerShell Gallery.
    • Perhaps this is a viable starting point: Creating a new module
  • Otherwise, use an aux. .NET SDK project to install your NuGet package, which is a nontrivial process outlined in this answer.

    • This MIT-licensed Gist contains helper function Add-NuGetType, which automates the process and additionally supports native library dependencies in PowerShell (Core) 7+, as discussed in this answer; however, note that the latter doesn't offer strict version control, and therefore shouldn't be used in production.
like image 151
mklement0 Avatar answered Oct 22 '25 19:10

mklement0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!