【pwsh】How to remove after Add-Type?

Table of contents

question

Add-TypeIt supports adding C#, VB, JavaScript, dll and other programming languages ​​as new .NET type 1 , and its scalability is extremely high.

However, it should be noted that Add-Typethe type cannot be removed, and once loaded in the same session window, repeated operations will only throw an error that the .NET type already exists.

solution

Referring to x0n 's answer in StackOverflowquestion 2 , loaded types are not unregistered until the session is closed. Charlie Kilian and Start-Automating cleverly use the type to start an additional thread, and use the new type to automatically log out after the end of the run. code show as below:Job

Start-Job -ScriptBlock {
    
    
    Add-Type -path 'my.dll'
    $myObj = New-Object My.MyTestClassName

    $result = $myObj.TestMethod
    $result
} | Receive-Job -Wait -AutoRemoveJob

reference


  1. Powershell commands detailed Add-Type ↩︎

  2. Can you remove an Add-ed Type in PowerShell again? ↩︎

Guess you like

Origin blog.csdn.net/qq_41755979/article/details/105669621