{"id":2121,"date":"2013-06-11T12:38:40","date_gmt":"2013-06-11T12:38:40","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2013\/06\/11\/understanding-powershells-type-conversion-magic\/"},"modified":"2019-02-18T13:05:33","modified_gmt":"2019-02-18T20:05:33","slug":"understanding-powershells-type-conversion-magic","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/understanding-powershells-type-conversion-magic\/","title":{"rendered":"Understanding PowerShell&#8217;s Type Conversion Magic"},"content":{"rendered":"<p>Type Conversion in PowerShell is, without question, one of the most useful ingredients in its &quot;Magic Sauce&quot; of administrator effectiveness. When you run a command with a specific parameter type (I.e.: a DateTime or a TimeSpan), things seem to &quot;just work&quot;.<\/p>\n<p>For example, consider this recent question \/ info dump on Twitter:<\/p>\n<blockquote class=\"twitter-tweet\">\n<p><a href=\"https:\/\/twitter.com\/search\/%23PowerShell\">#PowerShell<\/a> parameters that take a timespan object interpret 10 as 10 ticks, 10:00 as 10 hours, and &quot;10&quot; as 10 days.<\/p>\n<p> \u2014 June Blender (@juneb_get_help) <a href=\"https:\/\/twitter.com\/juneb_get_help\/status\/342469852960796672\">June 6, 2013<\/a><\/p><\/blockquote>\n<p> But how?<\/p>\n<p>Here are the steps that PowerShell takes on your behalf to convert input to a given type &#8211; such as TimeSpan. As with many things, there is no magic &#8211; just a lot of hard work.<\/p>\n<ol>\n<li><strong>Direct assignment.<\/strong> If your input is <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.type.isassignablefrom.aspx\">directly assignable<\/a>, simply cast your input to that type.<\/li>\n<li><strong>Language-based conversion.<\/strong> These language-based conversions are done when the target type is void, Boolean, String, Array, Hashtable, PSReference (i.e.: [ref]), XmlDocument (i.e.: [xml]). Delegate (to support ScriptBlock to Delegate conversions), and Enum.<\/li>\n<li><strong>Parse conversion.<\/strong> If the target type defines a Parse() method that takes that input, use that.<\/li>\n<li><strong>Static Create conversion.<\/strong> If the target type defines a static ::Create() method that takes that input, use that.<\/li>\n<li><strong>Constructor conversion.<\/strong> If the target type defines a constructor that takes your input, use that.<\/li>\n<li><strong>Cast conversion.<\/strong> If the target type defines a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/39bb81c3(v=vs.71).aspx\">implicit or explicit cast operator<\/a> from the source type, use that. If the source type defines an implicit or explicit cast operator to the target type, use that.<\/li>\n<li><strong>IConvertible conversion.<\/strong> If the <em>source type<\/em> defines an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.iconvertible.aspx\">IConvertible<\/a> implementation that knows how to convert to the target type, use that.<\/li>\n<li><strong>IDictionary conversion.<\/strong> If the source type is an IDictionary (i.e.: Hashtable), try to create an instance of the destination type using its default constructor, and then use the names and values in the IDictionary to set properties on the source object.<\/li>\n<li><strong>PSObject property conversion.<\/strong> If the source type is a PSObject, try to create an instance of the destination type using its default constructor, and then use the property names and values in the PSObject to set properties on the source object. . If a name maps to a method instead of a property, invoke that method with the value as its argument.<\/li>\n<li><strong>TypeConverter conversion.<\/strong> If there is a registered <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.componentmodel.typeconverter.aspx\">TypeConverter<\/a> or <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/system.management.automation.pstypeconverter(v=vs.85).aspx\">PSTypeConverter<\/a> that can handle the conversion, do that. You can register a TypeConverter through a types.ps1xml file (see: $pshome\\Types.ps1xml), or through <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/hh849908.aspx\">Update-TypeData<\/a>.<\/li>\n<\/ol>\n<p>Now, see if you can figure out what these do, and why:<\/p>\n<blockquote>\n<p><font size=\"2\" face=\"Courier New\">[TimeSpan] 10       <br \/> [TimeSpan] &quot;10&quot;        <br \/> [TimeSpan] &quot;0:10<\/font>&quot;      <\/p>\n<\/blockquote>\n<p>Hope this helps!<\/p>\n<p>&#160;<\/p>\n<p>Lee Holmes [MSFT]   <br \/>Windows PowerShell Development<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Type Conversion in PowerShell is, without question, one of the most useful ingredients in its &quot;Magic Sauce&quot; of administrator effectiveness. When you run a command with a specific parameter type (I.e.: a DateTime or a TimeSpan), things seem to &quot;just work&quot;. For example, consider this recent question \/ info dump on Twitter: #PowerShell parameters that [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2121","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>Type Conversion in PowerShell is, without question, one of the most useful ingredients in its &quot;Magic Sauce&quot; of administrator effectiveness. When you run a command with a specific parameter type (I.e.: a DateTime or a TimeSpan), things seem to &quot;just work&quot;. For example, consider this recent question \/ info dump on Twitter: #PowerShell parameters that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/2121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=2121"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/2121\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=2121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=2121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=2121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}