title: Loading Assemblies from NuGet Packages
body: <p>Sometimes in my PowerShell scripts, I need access to a specific DLL, using <code>Add-Type -AssemblyName</code>. However, the DLLs I need aren't always on the machine or in the GAC. For instance, I might want a quick script that uses Dapper to query a database. In these cases, I have been literally copying the DLLs along with the <code>ps1</code> file. I was wondering if this was common/a good idea and whether there was an existing extension that would load up NuGet packages, store then in a global or local folder and call <code>Add-Type -AssemblyName</code> automatically.</p>
  <p>It'd be a lot like using <code>npm</code> or <code>pip</code> in Node.js or Python, respectively.</p>
  <h2>Update</h2>
  <p>I did some research and there's nothing built-in to older versions of PowerShell. I made some progress trying to write one from scratch using the <code>nuget.exe</code></p>
  <pre><code>&amp;&quot;$(Get-Location)/nuget.exe&quot; install $packageName -Version $version -OutputDirectory &quot;$(Get-Location)/packages&quot; -NoCache -NoInteractive
  </code></pre>
  <p>This will download a given package/version under a &quot;packages&quot; folder in the current folder, along with any of its dependencies. However, it looks like it downloads every framework version, with no obvious way to tell which one to use for your given environment.</p>
  <p>Otherwise, you could just loop through the results and call Add-Type:</p>
  <pre><code>Get-ChildItem .\packages\ -Recurse -Filter &quot;*.dll&quot; | % {
      try
      {
          Add-Type -Path $_.FullName
      }
      catch [System.Exception]
      {
      }
  }
  </code></pre>
  <p>I tried using the <code>restore</code> command using a <code>project.json</code> file to see if I could control the framework version with no luck. This is just too hacky for me.</p>
  <p>I'll check out @crownedjitter's suggestion of using PowerShell 5.</p>
  <h2>Update</h2>
  <p>Using @crownedjitter's suggestion, I was able to eventually register the PackageManagement module with NuGet (see comments below). With the following command, I was able to reproduce what the <code>Nuget.exe</code> command above was doing:</p>
  <pre><code>Install-Package Dapper -Destination packages
  </code></pre>
  <p>Obviously, this is a lot shorter. Problem is it has the same limitation; it brings down every framework version of a package. If this includes .NET core, it brings down a good deal of the .NET core framework with it! There doesn't appear to be a way to specify a target framework (a.k.a, .NET 4.5.1 or below).</p>
  <p><a href="https://i.stack.imgur.com/poQ5g.png" rel="noreferrer"><img src="https://i.stack.imgur.com/poQ5g.png" alt="Install-Package grabbing dependencies for all .NET frameworks" /></a></p>
  <p>I am wondering if there is a way to determine which NuGet package folder(s) to load the DLLs from based on PowerShell's current <code>$PSVersionTable.CLRVersion</code> field.</p>