How to use interfaces in Powershell defined via Add-Type?

powershell interface
powershell classes in modules
powershell new-object
powershell add-type dll
powershell add-type remove
powershell add-type already exists
powershell object oriented
powershell add-type c#

I developed a PowerShell module that relied on a .NET Assembly for some operations. I refactored this module to not need that Assembly and noticed some strange behavior, which blocks me from removing the class at last:

Add-Type -TypeDefinition "public interface ICanTalk { string talk(); }" -Language CSharp

class Talker : ICanTalk {
    [string] talk() { return "Well hello there"; }
}

If you run this commands interactively, it will succeed. But as soon as I run it "en-bloque" in ISE or from a psm1 file, it will throw an error, stating it cannot find the interface defined in the Add-Type call.

I can reproduce the problem in both Windows-PowerShell and PowerShell Core (6.0.2)

What is the reason for the different behaviour and how can I tackle it?


Add-Type, But as soon as I run it "en-bloque" in ISE or from a psm1 file, it will throw an error, stating it cannot find the interface defined in the Add-Type call. To solve this problem, the Add-Type needs to be put into an own script file, which is run before the module loads (or in ISE, just run the code before running the class definitions). Add-Type. This can be accomplished by using ScriptsToProcess from the PSD1 file. Kudos to @TheIncorrigible1 for putting me on the track. To complement your own answer:


Apparently PowerShell will read the file and handle class-definitions before executing the code.

To solve this problem, the Add-Type needs to be put into an own script file, which is run before the module loads (or in ISE, just run the code before running the class definitions).

This can be accomplished by using ScriptsToProcess from the PSD1 file.

Kudos to @TheIncorrigible1 for putting me on the track.

about_Classes, that copies the file that the FileInfo object represents. PowerShell 5.0 added the ability to define classes and other user-defined types using formal syntax and semantics like other object-oriented programming languages. The goal is to enable developers and IT professionals to embrace PowerShell for a wider range of use cases, simplify development of PowerShell artifacts (such as DSC resources), and accelerate coverage of management surfaces.


Why not defining all classes in C# in a separate file, than add it as Add-Type -path MyClasses.cs This way it will work with older PS versions

about_Methods, PowerShell 5.0 adds a formal syntax to define classes and other user-defined types. Define custom types in PowerShell using familiar object-oriented Because interfaces allow multiple inheritance, a PowerShell class  The Add-Type cmdlet lets you define a Microsoft .NET Core class in your PowerShell session. You can then instantiate objects, by using the New-Object cmdlet, and use the objects just as you would use any .NET Core object. If you add an Add-Type command to your PowerShell profile, the class is available in all PowerShell sessions.


Executing C# code using PowerShell script – Random IT Utensils, Net code in PowerShell | Use existing code and even DLLs are no global variables available, that you defined in the PowerShell part of the script. Additionally, “Add-Type” must be at the very beginning of the script. Benefits of .​Net Code in PowerShell. There are various advantages using this technique. Using WMI. It is possible to use WMI and the Win32_NetworkAdapter WMI class to retrieve information about the connection status. The NetConnectionStatus property reports a coded value that reports the status. These values are documented on MSDN: Win32_NetworkAdapter class. By using the Get-WmiObject Windows PowerShell cmdlet,


PowerShell. Add-Type, I use explicit type defintion for all attributes of my custom classes. joeyaiello added Issue-Enhancement Up-for-Grabs and removed Issue-Question Defining a PowerShell class in a script causes a parser error if the class references external types that aren't NET Standard 2.1 custom interface #5691. This practically enables you to develop straight from PowerShell (although there is no designer.cs nor a visual interface). In the example I will open a dialog were the user can select a folder which then will be stored in a variable (only if the user clicked “OK”).


How to use .Net code in PowerShell (C# and DLL integration), But, Import-Module does not import PowerShell classes defined in the module. That is, importing the namespace and using the type name works only when typing the This isn't the same as the Add-Type cmdlet, which adds a class to the session that is NOT User interface design for administrators. @dpurge Yes the PowerShell class has to be parsed after the type is added. So having the class in the same file as the Add-Type won't work (because the whole file, including the class, is read before the Add-Type is executed). Putting the class into an external file will work because it delays reading the class until script execution time.