{"id":17791,"date":"2010-07-11T00:01:00","date_gmt":"2010-07-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/07\/11\/hey-scripting-guy-weekend-scripter-checking-for-module-dependencies-in-windows-powershell\/"},"modified":"2010-07-11T00:01:00","modified_gmt":"2010-07-11T00:01:00","slug":"hey-scripting-guy-weekend-scripter-checking-for-module-dependencies-in-windows-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-weekend-scripter-checking-for-module-dependencies-in-windows-powershell\/","title":{"rendered":"Hey, Scripting Guy! Weekend Scripter: Checking for Module Dependencies in Windows PowerShell"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. One of the cool things about Windows PowerShell is that if you do not like the way it does things, you can change it. If there is something that is lacking in the released product, you can often find it in a module that someone has written and posted to the web. There are several good modules available on the Internet that are worth exploring and worth using. <\/p>\n<p>One problem with using modules is you now have a dependency on external code, and this means that a script that uses the module must have the module installed or the script will fail. If you control the environment, taking an external dependency is not a bad thing; if you do not control the environment, an external dependency can be a disaster. <\/p>\n<p>One thing that can be used to check for dependencies is the <strong>#requires<\/strong> statement in Windows PowerShell 2.0. It can be used to check for the specific version of Windows PowerShell, a specific <strong>shellID<\/strong>, or a particular <strong>pssnapin<\/strong> (including major and minor version). Unfortunately, the <strong>#requires<\/strong> statement cannot be used to check for a required module. This is where my Get-MyModule.ps1 script comes into play. <\/p>\n<p><strong>Get-MyModule.ps1<\/strong><\/p>\n<p><span style=\"background-color: #f0f0f0\">Function Get-MyModule <br \/>{ <br \/>Param([string]$name) <br \/>if(-not(Get-Module -name $name)) <br \/>{ <br \/>if(Get-Module -ListAvailable | <br \/>Where-Object { $_.name -eq $name }) <br \/>{ <br \/>Import-Module -Name $name <br \/>$true <br \/>} #end if module available then import <br \/>else { $false } #module not available <br \/>} # end if not module <br \/>else { $true } #module already loaded <br \/>} #end function get-MyModule <br \/>get-mymodule -name &#8220;bitsTransfer&#8221;<\/span><\/p>\n<p>The <strong>Get-MyModule<\/strong> function accepts a single string&mdash;the name of the module to check. The <strong>if<\/strong> statement is used to see if the module is currently loaded. If it is not loaded, the <strong>Get-Module<\/strong> cmdlet is used to see if the module exists on the system. If it does exist, the module is loaded. <\/p>\n<p>If the module is already loaded in the current Windows PowerShell session, the <strong>Get-MyModule<\/strong> function returns <strong>$true<\/strong> to the calling code. Let&rsquo;s dig into the function in a bit more detail to see how it works. <\/p>\n<p>The first thing I do is use the <strong>if<\/strong> statement to see if the module is not loaded in the current session. To do this, I use the <strong>&ndash;not<\/strong> operator to see if the module is not loaded. The <strong>Get-Module<\/strong> cmdlet is used to search for the required module by name. This section of the script is shown here:<\/p>\n<p><span style=\"background-color: #f0f0f0\">Function Get-MyModule <br \/>{ <br \/>Param([string]$name) <br \/>if(-not(Get-Module -name $name)) <br \/>{ <\/span><\/p>\n<p>To obtain a list of modules that are installed on a system, use the <strong>Get-Module<\/strong> cmdlet with the <strong>&ndash;ListAvailable<\/strong> switch. Unfortunately, there is no way to filter the results, and this necessitates piping the results to the <strong>Where-Object<\/strong> cmdlet to see if the required cmdlet is installed on the system. If the module exists on the system, the function uses the <strong>Import-Module<\/strong> cmdlet to import the module, and it returns <strong>$true<\/strong> to the calling code. This section of the script is shown here:<\/p>\n<p><span style=\"background-color: #f0f0f0\">if(Get-Module -ListAvailable | <br \/>Where-Object { $_.name -eq $name }) <br \/>{ <br \/>Import-Module -Name $name <br \/>$true <br \/>} #end if module available then import<\/span><\/p>\n<p>The last two things to do in the function are to handle two other cases. If the module is not available, the <strong>Where-Object<\/strong> cmdlet will not find anything. This triggers the first <strong>else<\/strong> clause, where <strong>$false<\/strong> is returned to the calling code. If the module is already loaded, the second else clause returns <strong>$true<\/strong> to the script. This section of the script is shown here:<\/p>\n<p><span style=\"background-color: #f0f0f0\">else { $false } #module not available <br \/>} # end if not module <br \/>else { $true } #module already loaded <br \/>} #end function get-MyModule<\/span><\/p>\n<p>A simple use of the <strong>Get-MyModule<\/strong> function is to call the function and pass the name of a module to it. This example is actually seen in the last line of the Get-MyModule.ps1 script: <\/p>\n<p><span style=\"background-color: #f0f0f0\">get-mymodule -name &#8220;bitsTransfer&#8221;<\/span><\/p>\n<p>When called in this manner, the <strong>Get-MyModule<\/strong> function will load the <strong>bitstransfer<\/strong> module if it exists on your system and if it is not already loaded. If the module is already loaded or if it is loaded by the function, <strong>$true<\/strong> is returned to the script. If the module does not exist, <strong>$false<\/strong> is returned. The use of the <strong>Get-MyModule<\/strong> function is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7838.clip_image002_6382C3CA.jpg\"><\/a><\/p>\n<p><img decoding=\"async\" style=\"max-width: 500px;border: 0px\" alt=\"Image of using Get-MyModule function\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/july\/hey0711\/wes-07-11-10-01.jpg\" \/><\/p>\n<p>A better use of the <strong>Get-MyModule<\/strong> function is to use it as a prerequisite check for a script that uses a particular module. Your syntax might look something like this:<\/p>\n<p><span style=\"background-color: #f0f0f0\">If(Get-MyModule &ndash;name &ldquo;bitsTransfer&rdquo;) { call your bits code here } <\/span><\/p>\n<p><span style=\"background-color: #f0f0f0\">ELSE { &ldquo;Bits module is not installed on this system.&rdquo; ; exit }<\/span><\/p>\n<p>I will be using my new function in the coming days when I talk about using Windows PowerShell to interact with Group Policy. <\/p>\n<p>We invite you follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&nbsp;<\/p>\n<p><strong><\/strong><\/p>\n<p><strong>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here. One of the cool things about Windows PowerShell is that if you do not like the way it does things, you can change it. If there is something that is lacking in the released product, you can often find it in a module that someone has written and [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,52,3,4,61,45],"class_list":["post-17791","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-modules","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here. One of the cool things about Windows PowerShell is that if you do not like the way it does things, you can change it. If there is something that is lacking in the released product, you can often find it in a module that someone has written and [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17791","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=17791"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17791\/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=17791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}