none
Available Data Types in PowerShell

    Question

  • if I run:

    $MyTypes = add-type -path "D:\MyAssembly.dll" -PassThru

    $MyTypes will contain a list of objects that I could New-Object into existence.

    If I want a list of commands I can Get-Command.  

    Is there some way other than a passthru on a specific assembly to look at all of the Types available?  Or get NameSpaces for that matter?

    - Josh

    Wednesday, March 07, 2012 6:55 PM

Answers

  • $domain = [appdomain]::CurrentDomain
    $asms = $domain.GetAssemblies()
     
    $types = @()
     
    foreach($asm in $asms)
    {
       try{
       $types += $asm.gettypes() | select  name -Unique
       }
       catch{}
     
    }
    $types = $types | select name -Unique
     
    $types.count
     take a coffee break :)
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, March 07, 2012 8:21 PM
  • function Get-Type ([Regex]$Pattern = '.') {
      $ErrorActionPreference = 'SilentlyContinue'
      [AppDomain]::CurrentDomain.GetAssemblies() | ForEach-Object {
        $_.GetExportedTypes()
      } | Where-Object {$_ -match $Pattern} | Sort-Object FullName
    }
    
    # types whose fullname matches "Collections"
    $colTypes = Get-Type Collections
    
    # then you could group them by NameSpace and display or write to disk
    $colTypes | Format-Table FullName -GroupBy NameSpace -HideTableHeaders | more
    
    # all types (not necessarily all loaded)
    Get-Type | Format-Table FullName -GroupBy NameSpace -HideTableHeaders | more
    
    Wednesday, March 07, 2012 10:10 PM

All replies

  • Does this help at all?

    [Reflection.Assembly]::LoadFile(“D:\scripts\MyAssembly.dll”).gettypes()


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Wednesday, March 07, 2012 7:17 PM
  • $domain = [appdomain]::currentdomain
    $assemblies = $domain.getassemblies()
    $assemblies[0].gettypes()
     
    not exactly sure what you are looking for, but its in there, have fun
    digging :)
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, March 07, 2012 7:50 PM
  • $domain = [appdomain]::CurrentDomain
    $asms = $domain.GetAssemblies()
     
    $types = @()
     
    foreach($asm in $asms)
    {
       try{
       $types += $asm.gettypes() | select  name -Unique
       }
       catch{}
     
    }
    $types = $types | select name -Unique
     
    $types.count
     take a coffee break :)
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, March 07, 2012 8:21 PM
  • function Get-Type ([Regex]$Pattern = '.') {
      $ErrorActionPreference = 'SilentlyContinue'
      [AppDomain]::CurrentDomain.GetAssemblies() | ForEach-Object {
        $_.GetExportedTypes()
      } | Where-Object {$_ -match $Pattern} | Sort-Object FullName
    }
    
    # types whose fullname matches "Collections"
    $colTypes = Get-Type Collections
    
    # then you could group them by NameSpace and display or write to disk
    $colTypes | Format-Table FullName -GroupBy NameSpace -HideTableHeaders | more
    
    # all types (not necessarily all loaded)
    Get-Type | Format-Table FullName -GroupBy NameSpace -HideTableHeaders | more
    
    Wednesday, March 07, 2012 10:10 PM
  • Hi,

    The below links should be helpful:

    How To Load .NET Assemblies In A PowerShell Session

    http://www.dougfinke.com/blog/index.php/2010/08/29/how-to-load-net-assemblies-in-a-powershell-session/

    Add-Type

    http://technet.microsoft.com/en-us/library/dd315241.aspx

    about_Types.ps1xml

    http://technet.microsoft.com/en-us/library/dd347581.aspx

    Best Regards,

    Yan Li


    Yan Li

    TechNet Community Support

    Thursday, March 08, 2012 9:13 AM
    Moderator
  • Nice,

    Going to go with:

    function Get-Type ([Regex]$Pattern = '.') {
      $ErrorActionPreference = 'SilentlyContinue'
      [AppDomain]::CurrentDomain.GetAssemblies() | ForEach-Object {
        $_.GetExportedTypes()
      } | Where-Object {($_ -match $Pattern) -and $_.IsPublic} | Sort-Object FullName
    }

    All I added was the $_.IsPublic to take out classes that you shouldn't be able to use with New-Object.  I did look up the GetTypes (http://msdn.microsoft.com/en-us/library/system.reflection.assembly.gettypes.aspx) and GetExportedTypes (http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getexportedtypes.aspx).  It looks like Exported Types should filter out the IsPublic type information.  Looking at the examples that come up if I change to '-not $_.IsPublic' maybe these are mustinherit or some variation.  Either way, thanks for showing me where this is in the appDomain, was poking in there the other day. I should have  known that would be the goto place.

    Thursday, March 08, 2012 4:24 PM
  • so much fun in the framework its hard to keep track of!
     
    enjoy
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, March 08, 2012 5:31 PM
  • You're welcome Josh. I am not a developer but I tried both of your filter variations and noticed that the not public types all are nested types (they all have a plus sign), thanks for pointing this out. Here is a PSTeam post about them, in it there is a link to the .Net guidelines on nested types.

    Also, there is something I forgot to mention about the function, its parameter is constrained as Regex, this means that by default the pattern pass to is case sensitive, unless you use the IgnoreCase regex option (?i). To avoid this just change the type to String or do not include one at all.


    • Edited by zx38 Thursday, March 08, 2012 10:02 PM
    Thursday, March 08, 2012 10:01 PM