Getting a list of all types in the GAC with PowerShell

Surprisingly, it became necessary to list all the types in the assemblies loaded in the GAC. Getting the list of assemblies from the GAC isn’t hard, but then you need the types – and that requires some more programming.

I think this example greatly demonstrates the might of PowerShell. You can easily combine traditional text-based tools such as GacUtil with strong, object-oriented tools. The gap needs to be bridged, but built-in regular expression support goes pretty far to help you here.

Here’s the script:

& 'C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\gacutil.exe' /L | 
    where { $_ -match '^  ([\w\.]+,.*)$' } |
    foreach { 
       $assembly = [System.Reflection.Assembly]::Load($matches[1])
       $assembly.GetTypes() |
         where { $_.FullName -notmatch '[\+``]' } | # Strip off anonymous types and Generic suffixes
         foreach { new-object PSObject -prop @{ AssemblyName = $assembly.FullName; TypeName = $_.FullName } }
    } |
    sort AssemblyName, TypeName -unique |
    out-gridview

Of course, scripts like this are never perfect. Your need for a filter might vary; the example above strips off generated (“anonymous”) types and generics type name suffixes (`1 and so on). Then again, this is exactly why code like this is better off scripted than compiled: changing it should take very little effort.

April 16, 2010 В· Jouni Heikniemi В· No Comments
Tags:  В· Posted in: .NET

Leave a Reply