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

Time:11-17

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?

CodePudding user response:

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

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.
  • Related