title: How to load a custom DLL into PowerShell
body: <p>I've been trying to load a custom DLL into PowerShell with no luck. </p>
  
  <p>My DLL's are being created <a href="https://github.com/Kellen-Stuart/DnD" rel="nofollow noreferrer">based on this repository</a>. My end goal is to be able to load <code>Monster.dll</code> but I figured I'd start with <code>Dice.dll</code> (because it's a static class with practically no dependencies).</p>
  
  <p><strong>What I've tried</strong></p>
  
  <p>In PowerShell:</p>
  
  <p><code>cd C:\some\repo\dnd\dice\bin\Debug\netstandard2.1</code></p>
  
  <p><code>$path = (pwd).Path</code></p>
  
  <p><code>$path += "\Dice.dll"</code></p>
  
  <p><code>[Reflection.Assembly]::LoadFile("$path")</code></p>
  
  <pre><code>GAC    Version        Location
  ---    -------        --------
  False  v4.0.30319     C:\some\repo\dnd\dice\bin\Debug\netstandard2.1\Dice.dll
  </code></pre>
  
  <p>And then I try to call the simple Roll Dice function (which looks like this)</p>
  
  <p><code>[Dice.Dice]::Roll(20, 1)</code></p>
  
  <p>And it returns this error: </p>
  
  <pre><code>Unable to find type [Dice.Dice].
  At line:1 char:1
  + [Dice.Dice]::Roll(20, 1)
  + ~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (Dice.Dice:TypeName) [], RuntimeException
      + FullyQualifiedErrorId : TypeNotFound
  </code></pre>
  
  <p>The same thing happens if I try to call:</p>
  
  <p><code>[Dice]::Roll(20, 1)</code></p>
  
  <p>What am I doing wrong? </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Someone suggested that I try:</p>
  
  <p><code>Add-Type -Path "$path"</code></p>
  
  <p>and it returns this error: </p>
  
  <pre><code>Add-Type : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
  At line:1 char:1
  + Add-Type -Path "$path"
  + ~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [Add-Type], ReflectionTypeLoadException
      + FullyQualifiedErrorId : System.Reflection.ReflectionTypeLoadException,Microsoft.PowerShell.Commands.AddTypeCommand
  </code></pre>
  
  <p>The real "LoadExeption" is </p>
  
  <pre><code>Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its
  dependencies. The system cannot find the file specified.
  </code></pre>
  
  <p>Another suggestion from the comments was to run:</p>
  
  <p><code>[appdomain]::currentdomain.getassemblies()</code></p>
  
  <p>I was able to find this data from that command</p>
  
  <pre><code>@{CodeBase=file:///C:/some/Repo/DnD/dice/bin/Debug/netstandard2.1/Dice.dll; FullName=Dice,
  Version=1.0.0.0, Culture=neutral, PublicKeyToken=null; EntryPoint=; DefinedTypes=; Evidence=System.Security.Policy.Evidence;
  PermissionSet=&lt;PermissionSet class="System.Security.PermissionSet"
  version="1"
  Unrestricted="true"/&gt;
  ; SecurityRuleSet=Level2; ManifestModule=Dice.dll; ReflectionOnly=False;
  Location=C:\some\Repo\DnD\dice\bin\Debug\netstandard2.1\Dice.dll; ImageRuntimeVersion=v4.0.30319;
  GlobalAssemblyCache=False; HostContext=0; IsDynamic=False;
  EscapedCodeBase=file:///C:/some/Repo/DnD/dice/bin/Debug/netstandard2.1/Dice.dll; ExportedTypes=;
  IsFullyTrusted=True; CustomAttributes=; Modules=System.Reflection.RuntimeModule[]}
  </code></pre>
  
  <p>I also ran this command:</p>
  
  <pre><code>try {
      ([appdomain]::currentdomain.GetAssemblies() | where Location -match 'dice').gettypes()
  } catch {
      $_ | select *
  }
  </code></pre>
  
  <pre><code>PSMessageDetails      :
  Exception             : System.Management.Automation.MethodInvocationException: Exception calling "GetTypes" with "0" argument(s):
                          "Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more
                          information." ---&gt; System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested
                          types. Retrieve the LoaderExceptions property for more information.
                             at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
                             at System.Reflection.Assembly.GetTypes()
                             at CallSite.Target(Closure , CallSite , Object )
                             --- End of inner exception stack trace ---
                             at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext,
                          Exception exception)
                             at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
                             at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
                             at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
  TargetObject          :
  CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
  </code></pre>