Loading .NET Assemblies in a PowerShell Module

When using PowerShell, not all .NET assemblies are loaded by default in a new PowerShell session. Usually, one would do the following to gain access to an assembly such as System.Web:

Add-Type -AssemblyName System.Web

When working on a PowerShell module, this approach doesn’t work.  Even adding that code into one of the .ps1 file components of your module doesn’t seem to help. You still get an ‘Unable to find type’ error as per below:

image.png

The solution is to ensure that the following line is populated (and un-commented) in your module manifest:

RequiredAssemblies = @('System.Web')

This is all-the-more important with PowerShell 5.0 and newer when you’re working with classes, and some of the class members are based on external types.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s