{"id":13491,"date":"2011-06-28T00:01:00","date_gmt":"2011-06-28T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/06\/28\/combine-powershell-modules-to-avoid-writing-scripts\/"},"modified":"2011-06-28T00:01:00","modified_gmt":"2011-06-28T00:01:00","slug":"combine-powershell-modules-to-avoid-writing-scripts","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/combine-powershell-modules-to-avoid-writing-scripts\/","title":{"rendered":"Combine PowerShell Modules to Avoid Writing Scripts"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to combine Windows PowerShell modules to save time and reduce scripting overhead.<\/p>\n<p>&nbsp;<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. One of the cool things about Windows PowerShell modules is that they are easy to share. In addition, because the code is packaged as a module, one automatically knows what to do with the module, and how to use the code. If someone hands me a script, I have to read through it to figure out how to use it. Of course, I have to read through the code of a module before I read it, but that is simply a safety measure to ensure that I am not loading nefarious code into my computer.<\/p>\n<p>One of the things that immediately begins to bug me when I build a new computer is that Windows PowerShell is hidden in the Accessories menu. I mean it takes five clicks of the mouse, and one scroll of the mouse wheel to launch either Windows PowerShell or Windows PowerShell ISE. If I want both of them running, which I normally do, it takes 10 clicks of the mouse and two scrolls of the mouse wheel. Because I often close and open Windows PowerShell during the course of the day, it does not take too much before I have literally made several hundred mouse clicks and nearly a hundred mouse wheel scrolls. Not only does that waste a lot of time, but that also begins to take its toll on the wrist, elbow, and even shoulder.<\/p>\n<p>I <strong>know<\/strong> that I wrote a script a long time ago to pin an application to the taskbar, but for some reason I am unable to find it. I then remembered a module that was created by <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=Jan%20Egil%20Ring&amp;sections=7618\">Jan Egil Ring<\/a>. <a href=\"http:\/\/blog.powershell.no\/\">Jan writes a blog and has some pretty cool articles<\/a>. The PinApplications.psm1 module will pin and unpin applications from both the taskbar and from the Start menu. Luckily, Jan uploaded <a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/b66434f1-4b3f-4a94-8dc3-e406eb30b750\/\">the module to the Scripting Guys Script Repository<\/a>, so it is easy to locate. All I had to do was go to the page displaying the module, click Copy Code, and paste the module into my Windows PowerShell ISE. I like to save my scripts and modules in a scripts folder. This folder becomes my main script repository, and the files in here are my backup files. After I have saved the PinApplications.psm1 module file in its own folder, I use my <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/01\/20\/hey-scripting-guy-january-20-2010.aspx\">Copy-Modules.ps1 script<\/a> to install the module in my personal modules directory.<\/p>\n<p>One of the things that I have not had a chance to do is to figure out how to create my own libraries in Windows 7. I have spent a bit of time browsing MSDN, and did find an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/magazine\/dd861346.aspx\">article<\/a> that discussed working with the API.&nbsp;Unfortunately, there are no .NET Framework classes to do this. While I can use the <strong>Add-Type<\/strong> cmdlet to work with native code, the results are not very pretty. (See <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=pinvoke&amp;sections=7618\">these Hey, Scripting Guy! Blog posts for some excellent examples<\/a> of this type of scripting.)<\/p>\n<p>Anyway, luckily I ran across the <a href=\"http:\/\/windowslibrariespsh.codeplex.com\/\">Windows7Library for PowerShell project on Codeplex<\/a>. &nbsp;(Make sure you get the <a href=\"http:\/\/windowslibrariespsh.codeplex.com\/releases\/view\/68787\">updated version of the module<\/a>.&nbsp;Ragnar updated the module to version .5 based upon some feedback I had given him while I was writing this article.) This project, written by <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=ragnar%20harper&amp;sections=7618\">Ragnar Harper<\/a>, is discussed on <a href=\"http:\/\/blog.crayon.no\/blogs\/ragnar\/archive\/2009\/09\/02\/scripting-windows-7-libraries.aspx\">Ragnar&rsquo;s blog<\/a>. One limitation of my Copy-Modules.ps1 script, is that it only looks for modules and for manifest files. It does not look for *.xml, *.dll, or *.exe files. I did this on purpose because I wanted an easy installer for &ldquo;script modules&rdquo; not for more complex types of module projects. I figured that if I created a module project that relied on external *.dll&rsquo;s or *.exe files, I would create a Windows Installer project to do that. As seen in the following figure, Ragnar&rsquo;s Windows7Library module uses several different file types;&nbsp;this is because he relied upon a couple of other projects to simplify the creation of his module.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6765.HSG-06-28-11-01.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of Ragnar's module using different file types\" alt=\"Image of Ragnar's module using different file types\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6765.HSG-06-28-11-01.png\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>This does not mean that I cannot use my Copy-Modules.ps1 script to install the Windows7Library module. But it does mean that I will need to copy the remaining files into the folder once I run the script.<\/p>\n<p>AfterI have the two modules installed, I can write a quick script to pin my applications to the taskbar and to create my custom libraries.<\/p>\n<p>There are four default libraries created when Windows 7 is installed. These are stored in the profile. The four default libraries appear in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2133.HSG-06-28-11-02.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of default libraries in Windows 7\" alt=\"Image of default libraries in Windows 7\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2133.HSG-06-28-11-02.png\" \/><\/a><\/p>\n<p>My complete SetLibraryAndTaskBar.ps1 script is not too complicated. It includes two <strong>Import-Module<\/strong> statements, and then calls the <strong>Set-PinnedApplication<\/strong> function to add Windows PowerShell and the Windows PowerShell ISE to my taskbar. It then creates a library, and adds a folder to the library. In reality, I would not need to really create this &ldquo;script&rdquo; except that these are things I generally do to all of my workstations, and it saves a bit of typing.<\/p>\n<p style=\"padding-left: 30px\">Import-Module PinnedApplicationsModule<\/p>\n<p style=\"padding-left: 30px\">Import-Module Windows7Library<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Set-PinnedApplication -Action PinToTaskbar -FilePath $PSHOME\\powershell.exe<\/p>\n<p style=\"padding-left: 30px\">Set-PinnedApplication -Action PinToTaskbar -FilePath $PSHOME\\powershell_ise.exe<\/p>\n<p style=\"padding-left: 30px\">Add-Library -LibraryName &#8220;ScriptingGuys_2011&#8221;<\/p>\n<p style=\"padding-left: 30px\">Add-LibraryFolder -libraryName &#8220;ScriptingGuys_2011&#8221; -folderPath E:\\data\\ScriptingGuys\\2011<\/p>\n<p>&nbsp;<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to combine Windows PowerShell modules to save time and reduce scripting overhead. &nbsp; Microsoft Scripting Guy Ed Wilson here. One of the cool things about Windows PowerShell modules is that they are easy to share. In addition, because the code is packaged as a module, one automatically knows what to do with [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[16,47,52,3,4,45],"class_list":["post-13491","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-general-management-tasks","tag-modules","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to combine Windows PowerShell modules to save time and reduce scripting overhead. &nbsp; Microsoft Scripting Guy Ed Wilson here. One of the cool things about Windows PowerShell modules is that they are easy to share. In addition, because the code is packaged as a module, one automatically knows what to do with [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13491","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=13491"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13491\/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=13491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=13491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=13491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}