PowerShell - Set Alias for Loaded Assembly

前端 未结 4 764
傲寒
傲寒 2021-02-09 00:25

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, Publ         


        
4条回答
  •  情深已故
    2021-02-09 00:50

    You can add Powershell type accelerator (alias for type):

    $accel = [PowerShell].Assembly.GetType("System.Management.Automation.TypeAccelerators")
    $accel::add("mb","System.Windows.Forms.MessageBox")
    [mb]::Show("Hello world")
    

    More details can be found here and here.

    WIth PowerShell 5 you can also import namespaces:

    using namespace System.Windows.Forms
    [MessageBox]::Show("Hello world")
    

提交回复
热议问题