{"id":32,"date":"2020-01-06T14:00:45","date_gmt":"2020-01-06T14:00:45","guid":{"rendered":"https:\/\/www.practicalpowershell.com\/post\/working-with-packages-modules"},"modified":"2020-06-07T16:40:28","modified_gmt":"2020-06-07T22:40:28","slug":"working-with-packages-modules","status":"publish","type":"post","link":"https:\/\/practicalpowershell.com\/working-with-packages-modules\/","title":{"rendered":"Working with Packages\/Modules"},"content":{"rendered":"<p>Package and Module management can be a pretty important piece of using PowerShell to automate or to just run simple scripts to father information.  There are some nuances and things to remember when working with these parts of PowerShell.  Here are some tips and tricks I&#8217;ve run across: <\/p>\n<p><strong>(1) NuGet (Specific Package Example) <\/strong><\/p>\n<p>Querying to see if the module is installed and instead get a question back if I want to install it: <\/p>\n<p>Not exactly what I would have expected.  If I want to query for NuGet and get an answer as to whether it is installed, I need to do something like this: First, look for all PowerShell Package Providers:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\n$PackageProviders\u00a0=\u00a0(Get-PackageProvider\u00a0-ListAvailable).<br \/>\n[\/sourcecode]<br \/>\nName Then we can query the variable to see if the list of all providers contains NuGet:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nIf\u00a0($PackageProviders\u00a0-NotContains\u00a0&#039;NuGet&#039;){<br \/>\n[\/sourcecode]<br \/>\nIf it does, then we can check the version and move on to the next step or, if it is not in the list, we could install the latest version for example like so:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nInstall-PackageProvider\u00a0-Name\u00a0NuGet\u00a0-MinimumVersion\u00a02.8.5.201<br \/>\n\/[sourcecode]<br \/>\nNotice the -MinimumVersion parameter.  This can be useful if you are looking to guarantee a particular level of functionality from the item you are installing.  For the above example, the version specified was the minimum needed for another requirement in a script. <\/p>\n<p>&lt;strong&gt;(2) Installing Modules&lt;\/strong&gt;<br \/>\nIf you are not familiar with how PowerShell works, cmdlets are provided by Modules.  The PowerShell Modules contain groups of cmdlets that are then exposed and made visible depending on what rights you have according to RBAC.  Now, if you are working on a new server or jump box to manage a particular product, you may need to install new modules to make this possible,  These modules may be loaded manually by downloading the files and storing them in a folder like other modules are on that computer.  For example: <\/p>\n<p>Or, with newer versions of PowerShell, we can also install the modules with the PowerShell cmdlet &amp;#039;Install-Module&amp;#039;.  There are some caveats and things to remember with this method.  One, we need a newer version of PowerShell (5.0+).  We need permissions to install the module on the machine (could be blocked by GPO or an administrator) and this may need to be verified ahead of time.  We would also need to know the exact name of the module and possibly even the PowerShell Gallery that it can be downloaded from (some modules exist in multiple repositories).  Lastly, we may need to grant permission for PowerShell to download a module from that gallery with a one-liner like so:<br \/>\n[sourcecode language=&amp;quot;powershell&amp;quot;]<br \/>\nSet-PSRepository\u00a0-Name\u00a0&amp;quot;PSGallery&amp;quot;\u00a0-InstallationPolicyTrusted<br \/>\n[\/sourcecode]<br \/>\nOnce installed we should be able to import it and start working with the cmdlets it provides. <\/p>\n<p><strong>(3) Importing Modules (with Error Checking)<\/strong><br \/>\nWhen loading modules to be used, say with a script or just to run some cmdlets, it might be necessary to add some sort of error checking.  The error checking could be present to force a script to exit if the modules does not load or at the very least report the issue so that it can be fixed at a later time. <\/p>\n<p>An example of this could be trying to load the Active Directory PowerShell module.  We can wrap the import with a Try{}Catch{} code block like so:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\n$ADModuleLoad\u00a0=\u00a0$True<br \/>\nTry\u00a0{<br \/>\n    Import-Module\u00a0ActiveDirectory\u00a0-ErrorAction\u00a0STOP Write-Host &quot;Active\u00a0Directory\u00a0Module\u00a0was\u00a0loaded.&quot;<br \/>\n}\u00a0Catch\u00a0{<br \/>\n    Write-Host &quot;Active\u00a0Directory\u00a0Module\u00a0could\u00a0not\u00a0be\u00a0loaded!&quot;<br \/>\n    $ADModuleLoad\u00a0=\u00a0$False<br \/>\n}<br \/>\n[\/sourcecode]<br \/>\nWe can visually indicate the failure with the above code.<\/p>\n<p><strong>Caveats To The Above<\/strong><br \/>\n Some modules and even packages are subject to prerequisites.  You may need to walk-through multiple steps in order to get a new module installed.   You may even need to upgrade the PackageManagement package provider and this requires restarting the shell after it is complete.  So do not assume a new module install in always going to be quick and painless.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Package and Module management can be a pretty important piece of using PowerShell to automate or to just run simple scripts to father information. There are some nuances and things to remember when working with these parts of PowerShell. Here are some tips and tricks I&#8217;ve run across: (1) NuGet (Specific Package Example) Querying to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rop_custom_images_group":[],"rop_custom_messages_group":[],"rop_publish_now":"initial","rop_publish_now_accounts":[],"rop_publish_now_history":[],"rop_publish_now_status":"pending","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[22],"tags":[],"class_list":["post-32","post","type-post","status-publish","format-standard","hentry","category-powershell"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Working with Packages\/Modules - Practical PowerShell<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/practicalpowershell.com\/working-with-packages-modules\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Working with Packages\/Modules - Practical PowerShell\" \/>\n<meta property=\"og:description\" content=\"Package and Module management can be a pretty important piece of using PowerShell to automate or to just run simple scripts to father information. There are some nuances and things to remember when working with these parts of PowerShell. Here are some tips and tricks I&#8217;ve run across: (1) NuGet (Specific Package Example) Querying to [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/practicalpowershell.com\/working-with-packages-modules\/\" \/>\n<meta property=\"og:site_name\" content=\"Practical PowerShell\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-06T14:00:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-06-07T22:40:28+00:00\" \/>\n<meta name=\"author\" content=\"damian\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"damian\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/working-with-packages-modules\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/working-with-packages-modules\\\/\"},\"author\":{\"name\":\"damian\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"headline\":\"Working with Packages\\\/Modules\",\"datePublished\":\"2020-01-06T14:00:45+00:00\",\"dateModified\":\"2020-06-07T22:40:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/working-with-packages-modules\\\/\"},\"wordCount\":680,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"articleSection\":[\"PowerShell\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/practicalpowershell.com\\\/working-with-packages-modules\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/working-with-packages-modules\\\/\",\"url\":\"https:\\\/\\\/practicalpowershell.com\\\/working-with-packages-modules\\\/\",\"name\":\"Working with Packages\\\/Modules - Practical PowerShell\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#website\"},\"datePublished\":\"2020-01-06T14:00:45+00:00\",\"dateModified\":\"2020-06-07T22:40:28+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/working-with-packages-modules\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/practicalpowershell.com\\\/working-with-packages-modules\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/working-with-packages-modules\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/practicalpowershell.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Working with Packages\\\/Modules\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#website\",\"url\":\"https:\\\/\\\/practicalpowershell.com\\\/\",\"name\":\"Practical PowerShell\",\"description\":\"PowerShell books written by experts\",\"publisher\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/practicalpowershell.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\",\"name\":\"damian\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\",\"caption\":\"damian\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Working with Packages\/Modules - Practical PowerShell","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/practicalpowershell.com\/working-with-packages-modules\/","og_locale":"en_US","og_type":"article","og_title":"Working with Packages\/Modules - Practical PowerShell","og_description":"Package and Module management can be a pretty important piece of using PowerShell to automate or to just run simple scripts to father information. There are some nuances and things to remember when working with these parts of PowerShell. Here are some tips and tricks I&#8217;ve run across: (1) NuGet (Specific Package Example) Querying to [&hellip;]","og_url":"https:\/\/practicalpowershell.com\/working-with-packages-modules\/","og_site_name":"Practical PowerShell","article_published_time":"2020-01-06T14:00:45+00:00","article_modified_time":"2020-06-07T22:40:28+00:00","author":"damian","twitter_card":"summary_large_image","twitter_misc":{"Written by":"damian","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/practicalpowershell.com\/working-with-packages-modules\/#article","isPartOf":{"@id":"https:\/\/practicalpowershell.com\/working-with-packages-modules\/"},"author":{"name":"damian","@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"headline":"Working with Packages\/Modules","datePublished":"2020-01-06T14:00:45+00:00","dateModified":"2020-06-07T22:40:28+00:00","mainEntityOfPage":{"@id":"https:\/\/practicalpowershell.com\/working-with-packages-modules\/"},"wordCount":680,"commentCount":0,"publisher":{"@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"articleSection":["PowerShell"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/practicalpowershell.com\/working-with-packages-modules\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/practicalpowershell.com\/working-with-packages-modules\/","url":"https:\/\/practicalpowershell.com\/working-with-packages-modules\/","name":"Working with Packages\/Modules - Practical PowerShell","isPartOf":{"@id":"https:\/\/practicalpowershell.com\/#website"},"datePublished":"2020-01-06T14:00:45+00:00","dateModified":"2020-06-07T22:40:28+00:00","breadcrumb":{"@id":"https:\/\/practicalpowershell.com\/working-with-packages-modules\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/practicalpowershell.com\/working-with-packages-modules\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/practicalpowershell.com\/working-with-packages-modules\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/practicalpowershell.com\/"},{"@type":"ListItem","position":2,"name":"Working with Packages\/Modules"}]},{"@type":"WebSite","@id":"https:\/\/practicalpowershell.com\/#website","url":"https:\/\/practicalpowershell.com\/","name":"Practical PowerShell","description":"PowerShell books written by experts","publisher":{"@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/practicalpowershell.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb","name":"damian","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g","caption":"damian"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g"}}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/32","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":4,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"predecessor-version":[{"id":228,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/32\/revisions\/228"}],"wp:attachment":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}