Skip to content

/errors/powershell/unable-to-find-type

PowerShell: Unable to find type [System.Windows.Forms.MessageBox].

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
PowerShell
exit code
1
versions
Windows PowerShell 5.1, PowerShell 7.x
os
windows, linux, macos

Unable to find type [System.Windows.Forms.MessageBox]. means PowerShell evaluated a type literal in square brackets and no loaded assembly defines that type. Most often the assembly is simply not loaded: run Add-Type -AssemblyName System.Windows.Forms before using the type. Other causes are a typo in the namespace, a class from a module imported with Import-Module instead of using module, and types that exist only in Windows PowerShell 5.1 (.NET Framework) or only in PowerShell 7 (.NET).

Reproduce it

powershell
$ErrorActionPreference = 'Stop'
try { [System.Windows.Forms.MessageBoxButtons]::OKCancel } catch { $_.Exception.Message; '  -> ' + $_.FullyQualifiedErrorId }
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBoxButtons]::OKCancel
try { [Sytem.IO.Path]::GetExtension('a.txt') } catch { $_.Exception.Message }
[System.IO.Path]::GetExtension('a.txt')

Output

Unable to find type [System.Windows.Forms.MessageBoxButtons].
  -> TypeNotFound
OKCancel
Unable to find type [Sytem.IO.Path].
.txt

Assemblies that are not loaded by default

TypesLoad with
System.Windows.Forms.* (MessageBox, Form, OpenFileDialog)Add-Type -AssemblyName System.Windows.Forms
System.Drawing.* (Bitmap, Color)Add-Type -AssemblyName System.Drawing
System.Web.HttpUtilityAdd-Type -AssemblyName System.Web (Windows PowerShell 5.1)
System.IO.Compression.ZipFileAdd-Type -AssemblyName System.IO.Compression.FileSystem (5.1)
WPF types (System.Windows.Window, MessageBox in PresentationFramework)Add-Type -AssemblyName PresentationFramework

Use [System.AppDomain]::CurrentDomain.GetAssemblies() to list what is loaded in the session.

Types used before Add-Type runs

PowerShell resolves types in param() blocks, class definitions and attribute declarations when it parses the script, before the first line runs. An Add-Type at the top of the same script is too late for those uses. Load the assembly in the calling script or profile, or move the typed code into a function in a second file that you dot-source after Add-Type.

Classes from modules

powershell
using module MyTools       # must be the first statement in the script
[MyToolsConfig]::new()     # works; after Import-Module MyTools the class is not visible

Sources