{"id":4461,"date":"2009-02-06T14:36:00","date_gmt":"2009-02-06T14:36:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/02\/06\/requires-your-scripts\/"},"modified":"2019-02-18T13:12:48","modified_gmt":"2019-02-18T20:12:48","slug":"requires-your-scripts","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/requires-your-scripts\/","title":{"rendered":"#requires your scripts"},"content":{"rendered":"<p><span>Recently, I saw someone that had developed a script on&nbsp;the CTP3 drop&nbsp;and was then having trouble running it on v1 of PowerShell.&nbsp; Eventually it turned out that he was using v2 features in his script.<span>&nbsp; <\/span>Most of you know that we are trying to keep the next version of PowerShell compatible with v1 and I encourage you to report any problems that you might have in that area.<span>&nbsp; <\/span>Unfortunately, v2 being compatible with v1, doesn\u2019t mean v1 is compatible with v2. <\/span><\/p>\n<p><span>After seeing that, I&nbsp;started thinking about people sharing v2 scripts on the web and the problems they might cause someone that didn\u2019t realize the script was supposed to be run on v2.<span>&nbsp; <\/span>If you are sharing your v2 scripts (and I hope you are), then I wanted to say two things.<span>&nbsp; <\/span>Firstly, thanks!<span>&nbsp; <\/span>Sharing scripts helps the&nbsp;community&nbsp;tremendously.<span>&nbsp; <\/span>Secondly, if they are meant to be run only on v2, be sure to annotate them so people don\u2019t get into trouble trying to run them on v1.<\/span><\/p>\n<p><span>There are many ways you could annotate your scripts so people know what version they are supposed to be run on, but PowerShell already has a way to do it.<span>&nbsp; <\/span>It\u2019s called #requires.<span>&nbsp; <\/span>You can read all about it <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/dd315380.aspx\">here<\/a> or <a href=\"http:\/\/powershellers.blogspot.com\/2008\/06\/requires-statement.html\">here<\/a>.<span>&nbsp; <\/span>I\u2019m sure there are other places too, those are just the first ones I found.<\/span><\/p>\n<p><span>In the majority of cases, all it will amount to is putting \u201c#requires \u2013version 2\u201c<span>&nbsp; <\/span>at the top of your script.<span>&nbsp; <\/span>For example, here\u2019s my awesome script that will only run on v2.<\/span><\/p>\n<p class=\"MsoNormal\"><span>#requires -version 2<\/span><\/p>\n<p class=\"MsoNormal\"><span>write-host &#8220;Can only run on v2.&#8221;<\/span><\/p>\n<p><span>And here are the results on v2, followed by v1:<\/span><\/p>\n<p><b><span># V2<\/span><\/b><\/p>\n<p><span>PS&gt;.\\foo.ps1<\/span><\/p>\n<p><span>Can only run on v2.<\/span><\/p>\n<p class=\"MsoNormal\"><b><span># V1<\/span><\/b><\/p>\n<p class=\"MsoNormal\"><span>PS &gt; .\\foo.ps1<\/span><\/p>\n<p class=\"MsoNormal\"><span>The script &#8216;foo.ps1&#8217; cannot be run because it contained a &#8220;#requires&#8221; statement at line 1 for Windows PowerShell versi<\/span><\/p>\n<p class=\"MsoNormal\"><span>on 2.0 which is incompatible with the installed Windows PowerShell version of 1.0.<\/span><\/p>\n<p class=\"MsoNormal\"><span>At line:1 char:10<\/span><\/p>\n<p class=\"MsoNormal\"><span>+ .\\foo.ps1 &lt;&lt;&lt;&lt;<\/span><\/p>\n<p class=\"MsoNormal\"><span>PS &gt;<\/span><\/p>\n<p class=\"MsoNormal\"><span>&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\"><span>&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\"><span>&#8211; Marcel Ortiz Soto [MSFT]<\/span><span><\/span><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I saw someone that had developed a script on&nbsp;the CTP3 drop&nbsp;and was then having trouble running it on v1 of PowerShell.&nbsp; Eventually it turned out that he was using v2 features in his script.&nbsp; Most of you know that we are trying to keep the next version of PowerShell compatible with v1 and 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-4461","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>Recently, I saw someone that had developed a script on&nbsp;the CTP3 drop&nbsp;and was then having trouble running it on v1 of PowerShell.&nbsp; Eventually it turned out that he was using v2 features in his script.&nbsp; Most of you know that we are trying to keep the next version of PowerShell compatible with v1 and I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4461","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=4461"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4461\/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=4461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=4461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=4461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}