I use this code to load a .Net assembly to PowerShell:
[System.Reflection.Assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089") | out-null
[System.Windows.Forms.MessageBox]::Show("Hello world")
Can I set an alias for an assembly (for example: ‘System.Windows.Forms’ = ‘Forms’) so that I don’t have to type the assembly’s full name when calling a static methods like MessageBox.Show()?
While you can’t create some sort of namespace alias per se, you can use the following trick (taken from Lee Holmes’ PowerShell Cookbook):
But that only will work with
New-Objectsince that takes a string for the class name. You can’t use that syntax with a type name in square brackets.What you can do, however, is leave out the
Systempart which is implied:Makes it slightly shorter.