{"id":7181,"date":"2007-10-29T12:14:22","date_gmt":"2007-10-29T12:14:22","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2007\/10\/29\/dynamic-casting\/"},"modified":"2019-02-18T13:16:29","modified_gmt":"2019-02-18T20:16:29","slug":"dynamic-casting","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/dynamic-casting\/","title":{"rendered":"Dynamic Casting"},"content":{"rendered":"<p>We&#8217;ve seen a couple requests for this on our internal mailing lists so I thought other people would be interested.\n<\/p>\n<p>There are times when you want to do something like:\n<\/p>\n<p style=\"margin-left: 36pt\"><span style=\"font-family:Courier New\">$d = [DateTime]<br \/>[$d]&#8221;12\/25\/2007&#8243;<br \/>\n\t\t<\/span><\/p>\n<p>Or  (using a STRING instead of a TYPE):\n<\/p>\n<p style=\"margin-left: 36pt\"><span style=\"font-family:Courier New\">$d = Read-Host \u2013Prompt &#8220;TYPE:&#8221;<br \/>[$d]&#8221;12\/25\/2007&#8243;<br \/><\/span>\n\t<\/p>\n<p>Of course if either of these worked \u2013 I wouldn&#8217;t be writing this blog.  Jim Truher provided the solution:  the \u2013AS operator.\n<\/p>\n<p>The \u2013AS operator casts an object to a particular type.   Now in a traditional language, casting is thin operation which basically tells the compiler to think about things differently.  In PowerShell, the \u2013AS operator is a way to tell PowerShell want you want happen and then we do whatever we can do to make your wishes true.  (e.g. we do casting, coercion, conversion, etc).\n<\/p>\n<p style=\"margin-left: 36pt\"><span style=\"font-family:Courier New\">PS&gt;$d = [DateTime]<br \/>PS&gt;&#8221;12\/25\/2007&#8243; -as $d<\/p>\n<p>Tuesday, December 25, 2007 12:00:00 AM<\/p>\n<p>PS&gt;$d = Read-Host &#8220;Type&#8221;<br \/>Type: DateTime<br \/>PS&gt;&#8221;12\/25\/2007&#8243; -as $d<\/p>\n<p>Tuesday, December 25, 2007 12:00:00 AM<br \/>\n\t\t<\/span><\/p>\n<p>Enjoy!\n<\/p>\n<p>Jeffrey Snover [MSFT]<br \/>Windows Management Partner Architect<br \/>Visit the Windows PowerShell Team blog at:    <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a><br \/>Visit the Windows PowerShell ScriptCenter at:  <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a>\n\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;ve seen a couple requests for this on our internal mailing lists so I thought other people would be interested. There are times when you want to do something like: $d = [DateTime][$d]&#8221;12\/25\/2007&#8243; Or (using a STRING instead of a TYPE): $d = Read-Host \u2013Prompt &#8220;TYPE:&#8221;[$d]&#8221;12\/25\/2007&#8243; Of course if either of these worked \u2013 I [&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-7181","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>We&#8217;ve seen a couple requests for this on our internal mailing lists so I thought other people would be interested. There are times when you want to do something like: $d = [DateTime][$d]&#8221;12\/25\/2007&#8243; Or (using a STRING instead of a TYPE): $d = Read-Host \u2013Prompt &#8220;TYPE:&#8221;[$d]&#8221;12\/25\/2007&#8243; Of course if either of these worked \u2013 I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/7181","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=7181"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/7181\/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=7181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=7181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=7181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}