{"id":3264,"date":"2013-07-09T00:01:00","date_gmt":"2013-07-09T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/07\/09\/simplify-your-script-by-creating-powershell-type-accelerators\/"},"modified":"2013-07-09T00:01:00","modified_gmt":"2013-07-09T00:01:00","slug":"simplify-your-script-by-creating-powershell-type-accelerators","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/simplify-your-script-by-creating-powershell-type-accelerators\/","title":{"rendered":"Simplify Your Script by Creating PowerShell Type Accelerators"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Microsoft Scripting Guy, Ed Wilson, shows how easy it is to create custom Windows PowerShell type accelerators and incorporate them into scripts.<\/span>\nMicrosoft Scripting Guy, Ed Wilson, is here. Windows PowerShell is cool. In fact, it should come with a disclaimer something like this:<\/p>\n<p style=\"padding-left: 30px\">Warning! Windows PowerShell can be addictive. Symptoms of Windows PowerShell addiction include talking in verb-noun pairs; writing Windows PowerShell psudo-code on Facebook, Twitter, and in email. Advanced cases typically manifest themselves as dreaming of creating the perfect Windows PowerShell profile, and customizing your Windows PowerShell prompt to include current weather and stock information.\nAm I the only person who thinks that Windows PowerShell would be a great Twitter client? Oh well&hellip;\nWhen I was playing around with listing all type accelerators yesterday in my post, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/07\/08\/use-powershell-to-find-powershell-type-accelerators.aspx\" target=\"_blank\">Use PowerShell to Find PowerShell Type Accelerators<\/a>, I noticed that there is an <strong>Add<\/strong><em> <\/em>static method available.\nThis means that it should be possible to create and to add a custom type accelerator. To test this, I began playing around with different syntaxes until I came up with something that worked. Using the <strong>Get-Member<\/strong> cmdlet was enough to get me started in the right direction. This is shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; [accelerators] | get-member -Static -MemberType method -Name add | fl -Force<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">TypeName&nbsp;&nbsp; : System.Management.Automation.TypeAccelerators<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Add<\/p>\n<p style=\"padding-left: 30px\">MemberType : Method<\/p>\n<p style=\"padding-left: 30px\">Definition : static void Add(string typeName, type type)<\/p>\n<p style=\"padding-left: 30px\">I need to supply a string for <strong>TypeName<\/strong><em>, <\/em>and a type as a type. Hmmmm&hellip;<\/p>\n<p style=\"padding-left: 30px\">After a few tries, I came up with this syntax:<\/p>\n<p style=\"padding-left: 30px\">[accelerators]::add(&#8220;mydate&#8221;,&#8221;System.datetime&#8221;)\nThe first parameter becomes the name of the newly created type accelerator. The second string is the type that will be &ldquo;accelerated&rdquo; by the command. I now check to see if it works by simply placing the name in square brackets. As shown here, this appears to work:<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; [mydate]<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">IsPublic IsSerial Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BaseType<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;&#8211; &#8212;&#8212;&#8211; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">True&nbsp;&nbsp;&nbsp;&nbsp; True&nbsp;&nbsp;&nbsp;&nbsp; DateTime&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.ValueType\nNow I want to see if I can get the current date and time. I do this by calling the static <strong>Now<\/strong><em> <\/em>method. As shown here, it works:<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; [mydate]::now<\/p>\n<p style=\"padding-left: 30px\">Tuesday, July 2, 2013 1:44:00 PM\nGranted, all this is somewhat bogus because there is already the <strong>[datetime]<\/strong> type accelerator, but I was just taking the first step to see if I could make it work.<\/p>\n<h2>Creating a type accelerator for a non-loaded assembly<\/h2>\n<p>Inside the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/Microsoft.VisualBasic.aspx\" target=\"_blank\">Microsoft.VisualBasic assembly<\/a> are equivalents to many of the old VBScript functions. The Microsoft.VisualBasic assembly is not loaded by default. To use any of these classes, first load the assembly by using the <strong>Add-Type<\/strong> command. This command is shown here:<\/p>\n<p style=\"padding-left: 30px\">Add-Type -AssemblyName Microsoft.VisualBasic\nNow, I can add a type accelerator for the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.visualbasic.vbmath.aspx\" target=\"_blank\">Microsoft.VisualBasic.VBMath<\/a> .NET Framework class. I decide to call the accelerator VBMath. The script is shown here:<\/p>\n<p style=\"padding-left: 30px\">[accelerators]::Add(&#8216;vbmath&#8217;,&#8221;Microsoft.VisualBasic.vbmath&#8221;)<\/p>\n<p style=\"padding-left: 30px\">Now, I test out the newly created type accelerator:<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; [vbmath]<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">IsPublic IsSerial Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BaseType<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;&#8211; &#8212;&#8212;&#8211; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">True&nbsp;&nbsp;&nbsp;&nbsp; False&nbsp;&nbsp;&nbsp; VBMath&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Object\nIt returns, so that is good. Now I decide to pipe the object to the <strong>Get-Member<\/strong> cmdlet to see what it provides.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; [vbmath] | Get-Member -Static<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; TypeName: Microsoft.VisualBasic.VBMath<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType Definition<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">Equals&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static bool Equals(System.Object objA, System.Object o&#8230;<\/p>\n<p style=\"padding-left: 30px\">Randomize&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static void Randomize(), static void Randomize(double &#8230;<\/p>\n<p style=\"padding-left: 30px\">ReferenceEquals Method&nbsp;&nbsp;&nbsp;&nbsp; static bool ReferenceEquals(System.Object objA, System&#8230;<\/p>\n<p style=\"padding-left: 30px\">Rnd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; static float Rnd(), static float Rnd(float Number)\nIt looks like there are two interesting methods. (These methods are documented on MSDN). The first is <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/k47wsahf.aspx\" target=\"_blank\">Randomize<\/a>. I use it here:<\/p>\n<p style=\"padding-left: 30px\">[vbmath]::Randomize()\nNothing comes back because <strong>Randomize<\/strong> uses the system timer to obtain a seed value for the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/y66ey2hh.aspx\" target=\"_blank\">RND method<\/a>. <strong>RND<\/strong> returns a value that is less than 1 but greater than or equal to 0. If I do not use the <strong>Randomize<\/strong> method, the <strong>RND<\/strong> method always returns the same numbers in the same order. Now I use the <strong>RND<\/strong> method to create a few random numbers as shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; [vbmath]::Randomize()<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; [vbmath]::Rnd()<\/p>\n<p style=\"padding-left: 30px\">0.3871228<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; [vbmath]::Rnd()<\/p>\n<p style=\"padding-left: 30px\">0.2059224<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; [vbmath]::Rnd()<\/p>\n<p style=\"padding-left: 30px\">0.847851<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; [vbmath]::Rnd()<\/p>\n<p style=\"padding-left: 30px\">0.3145823<\/p>\n<h2>The point to all this<\/h2>\n<p>The point to all of this is that I can create a nice shortcut to a .NET Framework class, and thereby permit my code to be simpler and easier to read. Certainly <strong>[vbmath]<\/strong> is easier to read and type than <strong>[Microsoft.VisualBasic.VBMath]<\/strong>.\nIf I add the type and the type accelerator at the beginning of a script, my code is easier to read, and possibly easier to write. If I add the commands to my profile, I have access to them via the script and the Windows PowerShell command line. This is more flexible and offers more options.\nOf course, I would not create the <strong>[vbmath] <\/strong>type accelerator in real life. I have <strong>Get-Random<\/strong>, which is easier to use and is already built into Windows PowerShell (beginning with Windows PowerShell&nbsp;2.0). So there is no need to create an instance of this class. I am simply using it as an example of adding a type accelerator for something that does not already exist.\nThere are lots of useful .NET Framework classes that you might want to harvest for your scripts. MSDN documents them all. Between MSDN and Bing you have all you need. If you find something cool, please share it with us.\nThat is all there is to creating and to using type accelerators. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how easy it is to create custom Windows PowerShell type accelerators and incorporate them into scripts. Microsoft Scripting Guy, Ed Wilson, is here. Windows PowerShell is cool. In fact, it should come with a disclaimer something like this: Warning! Windows PowerShell can be addictive. Symptoms of Windows PowerShell [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[362,3,4,45,77],"class_list":["post-3264","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-powershell-3","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-writing"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how easy it is to create custom Windows PowerShell type accelerators and incorporate them into scripts. Microsoft Scripting Guy, Ed Wilson, is here. Windows PowerShell is cool. In fact, it should come with a disclaimer something like this: Warning! Windows PowerShell can be addictive. Symptoms of Windows PowerShell [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=3264"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3264\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=3264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}