{"id":3181,"date":"2010-01-23T09:59:00","date_gmt":"2010-01-23T09:59:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2010\/01\/23\/powershellhostversion-wtf\/"},"modified":"2019-02-18T13:06:01","modified_gmt":"2019-02-18T20:06:01","slug":"powershellhostversion-wtf","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/powershellhostversion-wtf\/","title":{"rendered":"PowerShellHostVersion &#8211; WTF?"},"content":{"rendered":"<p><P>Superstar Shay Levy recently wrote a blog article, <A href=\"http:\/\/blogs.microsoft.co.il\/blogs\/scriptfanatic\/archive\/2010\/01\/14\/module-manifest-gotcha.aspx\">Module Manifest Gotcha<\/A> .&nbsp; He was exploring the various options that the <A href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd878297(VS.85).aspx\">Module Manifest<\/A> provides and saw what he thought was a problem with using the PowerShellHostVersion key. <\/P>\n<DT><STRONG>PowerShellHostVersion key<\/STRONG><\/DT>\n<DD>Specifies the minimum version of the Windows PowerShell host program that works with the module.<\/DD>\n<P>The \u201cgotcha\u201d here is that there are LOTS of PowerShell Hosts:&nbsp; PowerShell.exe,&nbsp; Powershell_ISE.exe, <A href=\"http:\/\/www.powergui.org\/index.jspa\">PowerGUI<\/A>, Sapien\u2019s <A href=\"http:\/\/www.primaltools.com\/products\/info.asp?p=PrimalScript\">PrimalScript<\/A>, <A href=\"http:\/\/www.idera.com\/products\/powershell\/\">PowerShellPlus<\/A> and others (apologizes in advance to those I forgot to mention).&nbsp;&nbsp; Shay created a manifest with PowerShellHostVersion = \u201c2.0\u201d and it worked fine with PowerShell.exe but barfed when he imported it into PowerShell_ISE.exe (because that is Version 1.0).<\/P>\n<P>Say concluded: <\/P>\n<BLOCKQUOTE>\n<P><EM>\u201cTo be on the safe side, <STRONG>do not set a value<\/STRONG> for \u2018PowerShellHostVersion\u2019.\u201d<\/EM><\/P><\/BLOCKQUOTE>\n<P>That is almost, but not quite, right.&nbsp; To understand why we added this seemingly useless feature, you need to take a look at the other keys in the manifest ( <A href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd878297(VS.85).aspx\">HERE<\/A> ).&nbsp; There are about 15 keys you can specify there and in general,&nbsp; you can safely ignore most of them and everything will work fine.&nbsp; Most of these keys are put there to allow people that have specific requirements and would generate weird errors or unpredictable behavior if their requirements were not met.&nbsp; <\/P>\n<P>Let\u2019s take an example.&nbsp; <A href=\"http:\/\/technet.microsoft.com\/en-us\/library\/dd819474.aspx\">PowerShell_ISE<\/A> is our new Integrated Scripting Environment that we shipped with PowerShell V2.&nbsp; We are super excited with PowerShell_ISE but the fact of the matter is that it is a pretty lean tool without many features.&nbsp; Each of the tools I mentioned above blow the doors of PowerShell_ISE in terms of features (you should check them all out \u2013 they are great).&nbsp; We knew that there would be features that people needed that we were not delivering so we provided an extensibility mechanism, the <A href=\"http:\/\/technet.microsoft.com\/en-us\/library\/dd819478.aspx\">PowerShell ISE Scripting Object Model<\/A>.&nbsp; People do all sorts of wonderful things with this.&nbsp; For instance, James Brundage created ISE_Pack which is part of the (Free) Windows 7 Resource kit <A href=\"http:\/\/code.msdn.microsoft.com\/PowerShellPack\">PowerShell Pack<\/A>.&nbsp; This delivers a bunch of cool functions that only work with PowerShell_ISE (because they script against the PowerShell ISE Scripting Object).&nbsp; <\/P>\n<P>So what what happens when you try to import this module into plain old PowerShell.exe where they won\u2019t work because it doesn\u2019t have the PowerShell scripting model: <\/P>\n<P><FONT color=\"#0000ff\" size=\"2\" face=\"Courier New\">PS&gt; <STRONG>Import-Module isepack<\/STRONG> <BR>PS&gt;<\/FONT><\/P>\n<P>errrrr\u2026.. hmmm\u2026.. ooooookaaaaaay\u2026.. well <STRONG>that\u2019s<\/STRONG> embarrassing!&nbsp; <\/P>\n<P>I just loaded a module that has exactly 0% chance of working in this environment and got no error message.&nbsp; Let\u2019s see what happens if I try to do one of the functions:<\/P>\n<P><FONT color=\"#0000ff\" size=\"2\" face=\"Courier New\">PS&gt; <\/FONT><STRONG><FONT size=\"2\" face=\"Courier New\"><FONT color=\"#0000ff\">New-ScriptModuleFromCurrentLocation<\/FONT> <BR><\/FONT><\/STRONG><FONT color=\"#ff0000\" size=\"2\" face=\"Courier New\">Split-Path : Cannot bind argument to parameter &#8216;Path&#8217; because it is null. <BR>At C:\\Users\\jsnover\\Documents\\WindowsPowerShell\\Modules\\isepack\\New-ScriptModuleFromCurrentLocation.ps1:16 char:27 <BR>+&nbsp;&nbsp;&nbsp;&nbsp; $location = Split-Path &lt;&lt;&lt;&lt;&nbsp; $psise.CurrentFile.FullPath <BR>&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : InvalidData: (:) [Split-Path], ParameterBindingValidationException <BR>&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SplitPathCo <BR>&nbsp;&nbsp; mmand <\/FONT><\/P>\n<P><FONT color=\"#ff0000\" size=\"2\" face=\"Courier New\">Split-Path : Cannot bind argument to parameter &#8216;Path&#8217; because it is null. <BR>At C:\\Users\\jsnover\\Documents\\WindowsPowerShell\\Modules\\isepack\\New-ScriptModuleFromCurrentLocation.ps1:17 char:31 <BR>+&nbsp;&nbsp;&nbsp;&nbsp; $locationName = Split-Path &lt;&lt;&lt;&lt;&nbsp; $location -Leaf <BR>&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : InvalidData: (:) [Split-Path], ParameterBindingValidationException <BR>&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SplitPathCo <BR>&nbsp;&nbsp; mmand <\/FONT><\/P>\n<P><FONT color=\"#ff0000\" size=\"2\" face=\"Courier New\">Join-Path : Cannot bind argument to parameter &#8216;Path&#8217; because it is null. <BR>At C:\\Users\\jsnover\\Documents\\WindowsPowerShell\\Modules\\isepack\\New-ScriptModuleFromCurrentLocation.ps1:22 char:28 <BR>+&nbsp;&nbsp;&nbsp;&nbsp; $modulePath = Join-Path &lt;&lt;&lt;&lt;&nbsp; $location &#8220;$locationName.psm1&#8221; <BR>&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : InvalidData: (:) [Join-Path], ParameterBindingValidationException <BR>&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCom <BR>&nbsp;&nbsp; mand <\/FONT><\/P>\n<P><FONT color=\"#ff0000\" size=\"2\" face=\"Courier New\">Test-Path : Cannot bind argument to parameter &#8216;Path&#8217; because it is null. <BR>At C:\\Users\\jsnover\\Documents\\WindowsPowerShell\\Modules\\isepack\\New-ScriptModuleFromCurrentLocation.ps1:23 char:18 <BR>+&nbsp;&nbsp;&nbsp;&nbsp; if (Test-Path &lt;&lt;&lt;&lt;&nbsp; -ErrorAction SilentlyContinue $modulePath) { <BR>&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : InvalidData: (:) [Test-Path], ParameterBindingValidationException <BR>&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathCom <BR>&nbsp;&nbsp; mand <\/FONT><\/P>\n<P><FONT color=\"#ff0000\" size=\"2\" face=\"Courier New\">Exception calling &#8220;WriteAllText&#8221; with &#8220;2&#8221; argument(s): &#8220;Empty path name is not legal.&#8221; <BR>At C:\\Users\\jsnover\\Documents\\WindowsPowerShell\\Modules\\isepack\\New-ScriptModuleFromCurrentLocation.ps1:26 char:28 <BR>+&nbsp;&nbsp;&nbsp;&nbsp; [IO.File]::WriteAllText &lt;&lt;&lt;&lt; ($modulePath, $text) <BR>&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : NotSpecified: (:) [], MethodInvocationException <BR>&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : DotNetMethodException <\/FONT><\/P>\n<P><FONT color=\"#ff0000\" size=\"2\" face=\"Courier New\">You cannot call a method on a null-valued expression. <BR>At C:\\Users\\jsnover\\Documents\\WindowsPowerShell\\Modules\\isepack\\New-ScriptModuleFromCurrentLocation.ps1:27 char:42 <BR>+&nbsp;&nbsp;&nbsp;&nbsp; $psise.CurrentPowerShellTab.Files.Add &lt;&lt;&lt;&lt; ($modulePath) <BR>&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : InvalidOperation: (Add:String) [], RuntimeException <BR>&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : InvokeMethodOnNull<\/FONT><\/P>\n<P><FONT color=\"#0000ff\" size=\"2\" face=\"Courier New\">PS&gt;<\/FONT><\/P>\n<P>Ok \u2013 now I want to puke.&nbsp; <\/P>\n<P>What James has done is to highlight the \u201cFormality Range\u201d aspect of PowerShell.&nbsp; When you are writing code for yourself, it can be as informal as you like.&nbsp; PowerShell doesn\u2019t get in you way with lots of extra steps or hurdles \u2013 you can GO GO GO.&nbsp; But&nbsp; \u2026 when you want to share it with the world, PowerShell provides incremental features to be more formal and thus more friendly to a wide range of usage scenarios.&nbsp; <\/P>\n<P>What James should have done is to go into his manifest and change:<\/P>\n<P><FONT color=\"#0000ff\" size=\"2\" face=\"Courier New\">&nbsp;&nbsp;&nbsp; # Name of the Windows PowerShell host required by this module <BR>&nbsp;&nbsp;&nbsp; PowerShellHostName = &#8221;<\/FONT><\/P>\n<P>to<\/P>\n<P><FONT color=\"#0000ff\" size=\"2\" face=\"Courier New\">&nbsp;&nbsp;&nbsp; # Name of the Windows PowerShell host required by this module <BR>&nbsp;&nbsp;&nbsp; PowerShellHostName = &#8216;Windows PowerShell ISE Host&#8217;<\/FONT><\/P>\n<P>Once you do that and try to import the module for Powershell.exe (the console host), you get this error:<\/P>\n<P><FONT color=\"#0000ff\" size=\"2\" face=\"Courier New\">PS&gt; Import-Module isepack <BR><FONT color=\"#ff0000\">Import-Module : The name of the current PowerShell host is: &#8216;ConsoleHost&#8217;. The module &#8216;C:\\Users\\jsnover\\Documents\\Windo <BR>wsPowerShell\\Modules\\isepack\\isepack.psd1&#8242; requires the following PowerShell host: &#8216;Windows PowerShell ISE Host&#8217;. <BR>At line:1 char:5 <BR>+ ipmo &lt;&lt;&lt;&lt;&nbsp; isepack -Force <BR>&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : ResourceUnavailable: (C:\\Users\\jsnove&#8230;ck\\isepack.psd1:String) [Import-Module], Invalid <BR>&nbsp;&nbsp; OperationException <BR>&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : Modules_InvalidPowerShellHostName,Microsoft.PowerShell.Commands.ImportModuleCommand<\/FONT><\/FONT><\/P>\n<P>Now that is an error message that even I can understand!<\/P>\n<P>So where are we going with all of this?&nbsp; <\/P>\n<P>As I mentioned the first version of Powershell_ISE is pretty lean but we might go add more features in a future version.&nbsp; Let\u2019s imagine that James releases a new version of ISEPACK with a ton of new features that use the new PowerShell_ISE features.&nbsp; Since he added PowerShellHostName = &#8216;Windows PowerShell ISE Host&#8217;, we know that it will only load in the PowerShell_ISE but what happens if someone tries to load it into V1 of Powershell_ISE?&nbsp; We are going to be back to the same horrible experience of it loading OK only to fail in some random manner when I try to use one of it\u2019s features.&nbsp; <\/P>\n<P>But wait wait!&nbsp; <BR>Wouldn\u2019t it be great if James could specify that his new Module ONLY worked with V3 of PowerShell_ISE?&nbsp; <BR>Wouldn\u2019t it be great if there was a way to add something like:<\/P>\n<P><FONT color=\"#0000ff\" size=\"2\" face=\"Courier New\">&nbsp;&nbsp;&nbsp; # Name of the Windows PowerShell host required by this module <BR>&nbsp;&nbsp;&nbsp; PowerShellHostName = &#8216;Windows PowerShell ISE Host&#8217;<\/FONT><\/P>\n<P><FONT color=\"#0000ff\" size=\"2\" face=\"Courier New\">&nbsp;&nbsp;&nbsp; # Minimum version of the Windows PowerShell host required by this module <BR>&nbsp;&nbsp;&nbsp; <STRONG>PowerShellHostVersion = &#8216;3.0&#8217;<\/STRONG><\/FONT><\/P>\n<P>Then if you loaded it into V3, it would work and if you tried to&nbsp; load it into V1, it would give you a nice error message.&nbsp; <\/P>\n<P><FONT color=\"#0000ff\" size=\"2\" face=\"cou\">PS&gt;<STRONG> Import-Module ISEPACK<\/STRONG> <BR><FONT color=\"#ff0000\">Import-Module : The current PowerShell host is: &#8216;Windows PowerShell ISE Host&#8217; (version 2.0). The module &#8216;C:\\Users\\jsnover\\Documents\\WindowsPo <BR>werShell\\Modules\\ISEPACK\\ISEPACK.psd1&#8242; requires a minimum PowerShell host version of &#8216;3.0&#8217; to execute. <BR>At line:1 char:14 <BR>+ Import-Module &lt;&lt;&lt;&lt;&nbsp; ISEPACK -force <BR>&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : ResourceUnavailable: (C:\\Users\\jsnove&#8230;CK\\ISEPACK.psd1:String) [Import-Module], InvalidOperationException <BR>&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : Modules_InsufficientPowerShellHostVersion,Microsoft.PowerShell.Commands.ImportModuleCommand<\/FONT><\/FONT><\/P>\n<P><FONT color=\"#ff0000\"><\/FONT><\/P>\n<P>And now you know why we added something silly like PowerShellVersionHost to manifests.&nbsp; <BR>It\u2019s almost like we are thinking this stuff out.&nbsp; \ud83d\ude42<\/P>\n<P>Shay- thanks for bringing the topic up.&nbsp; It gave me an opportunity to explain what is going on and it clearly demonstrates that we need to beef up our documentation.&nbsp; We delivered SO MANY new functions in V2, that we have a lot of documentation to write.<\/P>\n<P>Experiment!&nbsp; Enjoy!&nbsp; Engage! <\/P>\n<P>Jeffrey Snover [MSFT] <BR>Distinguished Engineer <BR>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <A href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/A> <BR>Visit the Windows PowerShell ScriptCenter at:&nbsp; <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/A><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Superstar Shay Levy recently wrote a blog article, Module Manifest Gotcha .&nbsp; He was exploring the various options that the Module Manifest provides and saw what he thought was a problem with using the PowerShellHostVersion key. PowerShellHostVersion key Specifies the minimum version of the Windows PowerShell host program that works with the module. The \u201cgotcha\u201d [&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":[206,244,264,23],"class_list":["post-3181","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-isepack","tag-powergui","tag-powershell-plus","tag-sapien"],"acf":[],"blog_post_summary":"<p>Superstar Shay Levy recently wrote a blog article, Module Manifest Gotcha .&nbsp; He was exploring the various options that the Module Manifest provides and saw what he thought was a problem with using the PowerShellHostVersion key. PowerShellHostVersion key Specifies the minimum version of the Windows PowerShell host program that works with the module. The \u201cgotcha\u201d [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3181","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=3181"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3181\/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=3181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=3181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=3181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}