Search code examples
powershelladd-type

Determine in what module a class resides


I have some code that works a treat in the ISE, but fails in a script. It fails where I use [System.Windows.Media.GlyphTypeface]::new(), and this is a common occurrence, a module that is loaded automatically in the ISE, but needs to be loaded discretely in a script. So that brings up the generic question, is there a way to start from the type and determine what module is needed? Or is this one of those where you just need to already know and/or be able to mine the Microsoft support documents for the info?


Solution

  • System.Windows.Media.GlyphTypeface is not a PowerShell module. It is a .NET Framework class. You generally will need to check the documentation for the class to find the Assembly it belongs to. (https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.glyphtypeface) In this case PresentationCore.dll.

    You would use AddType to load this assembly.

    Add-Type -AssemblyName PresentationCore
    

    A search for "find .net assembly from class name" will find some Q&A on this topic, but mostly with C# examples.