Search notes:

PowerShell cmdLet Add-Type

The add-type cmdLet allows to load .NET framework classes.
Assemblies that were loaded into a PowerShell cannot be unloaded anymore and consequenctly, there is no remove-type cmdLet. There is no other way to get rid of such assemblies from memory but to restart PowerShell.

Adding an assembly

The -AssemblyName allows to add an assembly:
add-type -assemblyName Windows.Forms
Most of the time the following two lines are equivalent:
add-type -assemblyName Windows.Forms
[System.Reflection.Assembly]::System.Reflection.Assembly - LoadWithPartialName() method[LoadWithPartialName("Windows.Forms")

Adding a DLL to a PowerShell session

The following simple example tries to demonstrate how a DLL can be added to PowerShell session.
First, we need to create a DLL. We create it from the following source code
namespace TQ84 {
   public class T {
      public static int twice(int a) {
         return 2*a;
      }
   }
}
Github repository about-PowerShell, path: /cmdlets/type/add/tq84_type.cs
This source is compiled into a DLL with csc.exe, the C# compiler:
PS C:\path\to\somewhere> csc -nologo -target:library .\tq84_type.cs
The DLL can now be added to the current PowerShell session with the add-type cmdLet:
PS C:\path\to\somewhere> add-type -path 'tq84_type.dll'
We verify if the DLL was actually loaded. The following statement reports it as being loaded:
PS C:\path\to\somewhere> [AppDomain]::CurrentDomain.GetAssemblies()
Now that the DLL is loaded, we can use its functionality. Static methods can be invoked without creating an object:
PS C:\path\to\somewhere> [TQ84.T]::twice(21)
42
In order to invoke member methods, an instance of the class needs to be created. This can be done with new-object '….dll'

Loading a DLL with Reflection.Assembly

The .NET class System.Reflection.Assembly has the method LoadFile that is able to load DLLs as well. It is unclear to me if and what differences there are between using LoadFile and add-type. (Maybe Lee Holmes' blog might help):
PS C:\path\to\somewhere> [Reflection.Assembly]::LoadFile( (get-item .).fullName + '\tq84_type.dll')
PS C:\path\to\somewhere> [TQ84.T]::twice(21)

Option -typedef to add a class from C-Sharp

With the -typedef parameter, add-type can be given C# source code(!) to compile. add-type will then add the result to the current session:
add-Type -typedef @'
   using System;

   public class inl {
      public DateTime whenCreated;
      public inl() { whenCreated = DateTime.Now; }
   }
'@

$inl = new-object inl

start-sleep -s 3

write-output "It's $([DateTime]::Now) now, `$inl was created at $($inl.whenCreated)."
Github repository about-PowerShell, path: /cmdlets/type/add/inline.ps1

Adding a WinAPI function

The following snippet creates a type with a WinAPI function so that it can be called from PowerShell.
set-strictMode -version latest

$newType = add-type -name user32 -passThru -memberDefinition '
[DllImport("user32.dll", CharSet=CharSet.Auto)]
    public static extern Int32 MessageBox(
            IntPtr hWnd,
            String text,
            String caption,
            int    options
    );'


$MB_OKCANCEL = 4
$ID_YES      = 6
$ID_NO       = 7

$chosen = $newType::MessageBox(0, 'Should I?', 'Important question', $MB_OKCANCEL)

if ($chosen -eq $ID_YES) {
   write-host 'Yeah!'
}
elseif ($chosen -eq $ID_NO) {
   write-host 'Nay!'
}
else {
   write-host 'This is quite an unexpected answer.'
}
Github repository about-PowerShell, path: /cmdlets/type/add/WinApi.ps1
When executed, a simple yes-no Message Box is shown:
See also Create a Message Box with PowerShell

Option -codeDomProvider

TODO. This option is related to the System.CodeDom.Compiler.CodeDomProvider class.
Apparently, the -codeDomProvider option was removed with PowerShell version 6.

See also

The using assembly statement.
Powershell command noun: type
Creating assemblies with add-type and then using them
The Microsoft.PowerShell.Commands.AddType.AutoGeneratedTypes namespace.

Index