<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Media And Microcode : Get-Type</title><link>http://blogs.msdn.com/mediaandmicrocode/archive/tags/Get-Type/default.aspx</link><description>Tags: Get-Type</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Microcode: Exploring More of .NET with Get-Assembly</title><link>http://blogs.msdn.com/mediaandmicrocode/archive/2008/11/08/microcode-exploring-more-of-net-with-get-assembly.aspx</link><pubDate>Sat, 08 Nov 2008 03:46:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9053397</guid><dc:creator>JamesBrundage</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/mediaandmicrocode/comments/9053397.aspx</comments><wfw:commentRss>http://blogs.msdn.com/mediaandmicrocode/commentrss.aspx?PostID=9053397</wfw:commentRss><description>&lt;p&gt;In a previous post, I introduced a function called &lt;a href="http://blogs.msdn.com/mediaandmicrocode/archive/2008/10/23/microcode-powershell-scripting-tricks-exploring-net-types-with-a-get-type-function-and-reflection.aspx"&gt;Get-Type&lt;/a&gt;, which allows you to see all of the types currently loaded by .NET.&amp;#160; What it doesn't do is help you find assemblies, the DLL files containing new types.&amp;#160; So while there might be 19000-odd types loaded when I launch the Windows PowerShell Integrated Script Editor (aka Graphical PowerShell), it's the tip of the iceberg for what .NET can do.&lt;/p&gt;  &lt;p&gt;Since the Iceberg is so huge, you probably do not want to try to load every bit of the iceberg just to see what it contains.&amp;#160; So you're left with a classic &lt;a href="http://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat"&gt;Schr&amp;#246;dinger's Cat&lt;/a&gt; problem (observing something may change what you observe).&lt;/p&gt;  &lt;p&gt;Luckily, assemblies are located in a common spot on the filesystem, so it's actually really easy to find all of the assemblies that exist for public consumption without loading them.&amp;#160; This will catch assemblies installed for public use, but it will not catch an assembly that lives in the same directory as a program.&lt;/p&gt;  &lt;p&gt;In this post, I'll show you how to write a Get-Assembly function, and use it with the Object Pipeline to explore and load up specific assemblies.&amp;#160; Since Assemblies exist underneath the Windows Directory, I can search the directory with Get-ChildItem (or dir, if you prefer).&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;Get-ChildItem (Join-Path $env:Windir &amp;quot;Assembly&amp;quot;) -recurse -filter &amp;quot;*.dll&amp;quot;&lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;If you run this, you should notice two things.&amp;#160; First, there's a lot of output that goes by far too quickly to be useful to a person. Second, there's a bunch of files DLLs, and some files named something like &lt;em&gt;Assembly.Ni.Dll&lt;/em&gt;.&amp;#160; We cannot load those assemblies, so we'll remove them from the list by adding on a Where-Object.&amp;#160; Then, to make loading up the assembly easier, I'll pipe each result to Add-Member in order to add a method to load the assembly.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;function Get-Assembly()        &lt;br /&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get-ChildItem (Join-Path $env:Windir &amp;quot;Assembly&amp;quot;) -recurse -filter &amp;quot;*.dll&amp;quot; |         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Where-Object {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ! $_.Name.Substring(0,$_.Name.IndexOf($_.Extension)).EndsWith(&amp;quot;.ni&amp;quot;)         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } | Add-Member ScriptMethod Load -passThru {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [Reflection.Assembly]::LoadFrom($this.FullName)         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }         &lt;br /&gt;}&lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This Get-Assembly gets rid of the DLLs we can't use, but it's still not ideal for human consumption because of how files are displayed.&amp;#160; Files from different directories show up with a header for that directory, so running this produces a lot of white space when you're really interested in the name or the full path. Luckily, I can always summarize the object with Select-Object.&amp;#160; The next bit will sort all of the assemblies by their name and output just that property.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;Get-Assembly |        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Sort-Object Name |         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Select-Object Name&lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;I can also use the Get-Assembly command to load up assemblies to explore with Get-Type.&amp;#160; This next statement will walk through all of the assemblies related to Linq and load them up:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;Get-Assembly |        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Where-Object { $_.Name -like &amp;quot;*Linq*&amp;quot; } |         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Foreach-Object { $_.Load() } &lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;I can also save the loaded assemblies into a variable so I can use them later.&amp;#160; For instance, I'll save the Linq assemblies and then use Get-Type to look through each of the types in those assemblies.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;$assemblies = Get-Assembly |        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Where-Object { $_.Name -like &amp;quot;*Linq*&amp;quot; } |         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Foreach-Object { $_.Load() }         &lt;br /&gt;&lt;/em&gt;&lt;/p&gt;    &lt;p&gt;&lt;em&gt;Get-Type | Where-Object {        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; $type = $_         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; $assemblies | Where-Object { $type.Assembly -eq $_ }         &lt;br /&gt;}&lt;/em&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Get-Type and Get-Assembly work well together as a way to explore all that .NET has to offer.&amp;#160; On my box, there's over 500 assemblies, of which only 41 are currently loaded in PowerShell.&amp;#160; If 41 assemblies contain about 20000 types, imagine just how many pre-canned solutions exist in .NET just waiting for you to use.&lt;/p&gt;  &lt;p&gt;Hope this Helps,&lt;/p&gt;  &lt;p&gt;James Brundage [MSFT]&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Download scripts:&lt;/b&gt;&lt;/p&gt;  &lt;table border='1'&gt;&lt;theader&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;strong&gt;Module Name&lt;/strong&gt;&lt;/td&gt;        &lt;td&gt;&lt;strong&gt;Scripts&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt; &lt;/theader&gt;&lt;/tbody&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;DotNet&lt;/td&gt;        &lt;td&gt;&lt;a href="http://cid-2b8a402d0ba15e82.skydrive.live.com/self.aspx/PowerShell%20Scripts/DotNet/Get-Assembly.ps1"&gt;Get-Assembly&lt;/a&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;&amp;#160;&lt;/td&gt;        &lt;td&gt;&lt;a href="http://cid-2b8a402d0ba15e82.skydrive.live.com/self.aspx/PowerShell%20Scripts/DotNet/Get-Type.ps1"&gt;Get-Type&lt;/a&gt;&lt;/td&gt;    &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9053397" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/PowerShell/default.aspx">PowerShell</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/Get-Type/default.aspx">Get-Type</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/Get-Assembly/default.aspx">Get-Assembly</category></item><item><title>Microcode: PowerShell Scripting Tricks - Exploring .NET Types with a Get-Type function and Reflection</title><link>http://blogs.msdn.com/mediaandmicrocode/archive/2008/10/23/microcode-powershell-scripting-tricks-exploring-net-types-with-a-get-type-function-and-reflection.aspx</link><pubDate>Fri, 24 Oct 2008 01:16:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9014243</guid><dc:creator>JamesBrundage</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/mediaandmicrocode/comments/9014243.aspx</comments><wfw:commentRss>http://blogs.msdn.com/mediaandmicrocode/commentrss.aspx?PostID=9014243</wfw:commentRss><description>&lt;p&gt;There's a simple yet powerful function that nearly everyone on the PowerShell team has written a version of.&amp;#160; My version is called Get-Type.&amp;#160; It's only a one-liner, but it's an amazing way to explore .NET and it's also an amazing example of some of the things you can do with the object pipeline.&amp;#160; What my version does is get all of the existing .NET types loaded in the current program.&amp;#160; In this post, I'll introduce the basic function, and I'll build upon it in order to show some cool things you can do with .NET reflection.&amp;#160; I first introduced this function in the &lt;a href="http://blogs.msdn.com/powershell/archive/2008/05/23/wpf-powershell-part-2-exploring-wpf-and-the-rest-of-net-with-scripts.aspx"&gt;2nd post of my series on WPF&lt;/a&gt;, but I didn't really show that much .NET fun with it.&lt;/p&gt;  &lt;p&gt;Here's a simple version:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;function Get-Type() {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [AppDomain]::CurrentDomain.GetAssemblies() |&amp;#160; Foreach-Object { $_.GetTypes() }       &lt;br /&gt;}&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Go ahead and run it.&amp;#160; Running it on CTP2 of Graphical PowerShell on my home box, I got 19303 .NET types.&amp;#160; That's over 19000 things you can build upon.&amp;#160; 19000 potential solutions sitting right there at your fingertips.&amp;#160; A remarkable amount of information can be gleaned just looking directly at the types, and it's a lot faster than looking it up on MSDN (although MSDN is a great companion to this knowledge).&lt;/p&gt;  &lt;p&gt;How I figured out there were 19303 .NET types is a great example of the object pipeline:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Get-Type | Measure-Object&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;But that's only scratching the surface of what we can do with the rich data of .NET and PowerShell's powerful object pipeline.&lt;/p&gt;  &lt;p&gt;Let's suppose that I want to find every Xml-related type:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Get-Type | ? { $_.Name -like &amp;quot;*Xml*&amp;quot; } &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Now suppose I want to find the constructors of the constructors of the objects:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Get-Type | ? { $_.Name -like &amp;quot;*Xml*&amp;quot; } | % { $_.GetConstructors() }&amp;#160; | %&amp;#160; { &amp;quot;$_&amp;quot; }&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Now suppose I want to check out what each constructor can do:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Get-Type | ? { $_.Name -like &amp;quot;*Xml*&amp;quot; } | % { $_.GetConstructors() } | Get-Member&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Looking at this, I can see that constructors contain a GetParameters() method.&amp;#160; Let's put that together with Get-Type to build something that will find all of the types that I can build using that type.&lt;/p&gt;  &lt;p&gt;function Search-Constructor([Type]$parameterType, $namespace) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get-Type |     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Where-Object { $_.Namespace -like &amp;quot;*$namespace*&amp;quot; } |&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Where-Object {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $_.GetConstructors() |     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Foreach-Object {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $_.GetParameters() |     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Where-Object {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $_.ParameterType -eq $parameterType     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/p&gt;  &lt;p&gt;It will probably take a little bit to run (on my machine, it took about 30 seconds to find anything that used XmlDocument in its constructor, with no namespace specified) , but you can easily use this to start discovering how .NET types interact.&lt;/p&gt;  &lt;p&gt;Similar functions will let you find Properties, Events, and Methods:&lt;/p&gt;  &lt;p&gt;function Search-Method([Type]$parameterType, $namespace) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Get-Type |&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Where-Object { $_.Namespace -like &amp;quot;*$namespace*&amp;quot; } |     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Where-Object {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $_.GetMethods() |     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Foreach-Object {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $_.GetParameters() |     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Where-Object {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $_.ParameterType -eq $parameterType     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;If you want to check out any property, just use Select-Object to view the type.&amp;#160; (You can also summarize the object by adding hashtables, for instance, this expression will output each type's fullname and all of their methods:&lt;/p&gt;  &lt;p&gt;Get-Type | Select-Object FullName, @{Name='Methods';Expression={$_.GetMethods() | % {$_.ToString()}}}&lt;/p&gt;  &lt;p&gt;This is a great example of the usefulness of the Object pipeline.&amp;#160; Instead of getting a list of all of the typenames, you can delve deep into the what a type can do for you.&amp;#160; The more you explore .NET.&amp;#160; These little snippets should help get you started, but the sky's the limit.&amp;#160; Try writing a few of your own. The more you explore .NET, the more you'll be amazed at the variety of code that exists to solve your everyday programming problems.&lt;/p&gt;  &lt;p&gt;Hope this helps,&lt;/p&gt;  &lt;p&gt;James Brundage [MSFT]&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9014243" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/PowerShell/default.aspx">PowerShell</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/Microcode/default.aspx">Microcode</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/Select-Object/default.aspx">Select-Object</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/Scripting+Tricks/default.aspx">Scripting Tricks</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/Where-Object/default.aspx">Where-Object</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/Get-Type/default.aspx">Get-Type</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/Reflection/default.aspx">Reflection</category></item></channel></rss>