By default, Powershell loads a bunch of assemblies for you so that you can start working with the base types, and some common functions. If you want to use the functionality in an assembly that is not already loaded, you will have to load that assembly yourself. I will describe this process in this blog.
Firstly, lets look at what assemblies are loaded by default.
[System.Threading.Thread]::GetDomain().GetAssemblies()
Produces the following output in my Powershell instance (Sorry about the small font, there is a lot to fit in. You can run this on your PC to get your own results.)
GAC Version Location
--- ------- --------
True v2.0.50727 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\Microsoft.PowerShell.ConsoleHost\1.0.0.0
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\System.Configuration.Install\2.0.0.0__b03f5
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Management\1.
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\Microsoft.PowerShell.Security\1.0.0.0__31bf
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Utility\1.0.0
True v2.0.50727 C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\System.DirectoryServices\2.0.0.0__b03f5f7f1
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\System.Management\2.0.0.0__b03f5f7f11d50a3a
False v2.0.50727 C:\Program Files\PowerShell Community Extensions\Pscx.dll
False v2.0.50727 C:\Program Files\PowerShell Community Extensions\Pscx.Core.dll
False v2.0.50727 C:\Program Files\PowerShell Community Extensions\ICSharpCode.SharpZipLib
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50
True v2.0.50727 C:\WINDOWS\assembly\GAC_32\System.Transactions\2.0.0.0__b77a5c561934e089
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\System.Data.SqlXml\2.0.0.0__b77a5c561934e08
So now you want to load another assembly. There are several methods you can use o load an assembly. The first one is to know the full name of the assembly, including the assembly name including namespace, the version, culture, and the Public Key Token if signed.
[Reflection.Assembly]::Load("System.Windows.Forms, Version=2.0.0.0, " `
+ "Culture=neutral, PublicKeyToken=b77a5c561934e089")
A slightly easier method is to load the assembly from a fully qualified path
[Reflection.Assembly]::LoadFrom("C:\WINDOWS\assembly\GAC\System.Windows" `
+ ".Forms\1.0.5000.0__b77a5c561934e089\System.Windows.Forms.dll")
Here is an easy way to list all of the fully qualified path names for all assemblies in the GAC
$GacRootDir = Join-Path -Path $Env:SystemRoot -ChildPath "Assembly/Gac"
Get-Childitem -path $GacRootDir -recurse -include *.dll| %{$_.FullName}
And now, taking that one step further, you could load all assemblies in the GAC with the following Powershell commands
$GacRootDir = Join-Path -Path $Env:SystemRoot -ChildPath "Assembly/Gac"
Get-Childitem -path $GacRootDir -recurse -include *.dll |
Foreach-Object {$_.FullName} |
Foreach-Object {([Reflection.Assembly]::LoadFrom($_))}
If you do not want to display the success and fail message, you can assign the success return objects into $null, and set up a trap block that just continues to the next execution line if an exception ocurrs.
$GacRootDir = Join-Path -Path $Env:SystemRoot -ChildPath "Assembly/Gac"
Get-Childitem -path $GacRootDir -recurse -include *.dll |
Foreach-Object {$_.FullName} |
Foreach-Object {
trap {continue} $null = ([Reflection.Assembly]::LoadFrom($_))
}
The assemblies do not have to be placed in the GAC in order to be loaded. You can specify the path to any of your own .Net assemblies by supplying the path to that assembly.
Posted
May 28 2007, 10:01 PM
by
David