Leveraging Windows PowerShell Type Extensions to get documentation

After just a little use of Windows PowerShell, you quickly learn that you need to pipe objects in the GET-MEMBER utility to understand the capabilities of that object.  Get-Member reflects against the object and shows you all of its methods and properties (more as well but that will be a different blog).  Get-Member is getting that information from the metadata about the type but the issue is that the metadata doesn’t have any documenation about what the properties are or what the methods do.  Now mostly that doesn’t matter because it is obvious but occassionally it does.  Here is how I handle that problem;


Below is an XML fragment from my MY.TYPES.PS1XML file that gets loaded during startup.  In my profile is a line:


Update-TypeData c:\ps\My.Types.Ps1xml


If you want to see an example of how this file needs to be written, you can do the following:


notepad  (join-path $pshome types.ps1xml)


Anyway – one of my entries is this:


<Type>
    <Name>System.Object</Name>
    <Members>
      <ScriptMethod>
        <Name>MSDN</Name>
        <Script>
if (($global:MSDNViewer -eq $null) -or ($global:MSDNViewer.HWND -eq $null))
{   $global:MSDNViewer = new-object -ComObject InternetExplorer.Application
}
$Uri = “http://msdn2.microsoft.com/library/” + $this.GetType().FullName + “.ASPX”
$global:MSDNViewer.Navigate2($Uri)
$global:MSDNViewer.Visible = $TRUE
        </Script>
      </ScriptMethod>
    </Members>
</Type>


 


Then I can do things like:


$d=get-date
$d.MSDN()


and it will fire up IE and show the documentation for this type.
Note that I attached this property to System.Object so absolutely every object now has this method.  Pretty cool huh?


Jeffrey Snover
Windows PowerShell Architect
(Can you believe that they actually pay to work on this Windows PowerShell – is that a deal or what!  We’re still hiring http://blogs.msdn.com/powershell/archive/2006/05/04/590382.aspx)


PSMDTAG:FAQ: How can I find out what an Object can do?
PSMDTAG:FAQ: How can I get at the MSDN documenation for an object?
PSMDTAG:TYPEEXTENSION: MSDN Method on System.Object
PSMDTAG:DOCUMENTATION: Adding a method to all objects to get their MSDN Documentation