{"id":16801,"date":"2010-10-16T00:01:00","date_gmt":"2010-10-16T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/10\/16\/learn-how-to-load-and-use-powershell-snap-ins\/"},"modified":"2010-10-16T00:01:00","modified_gmt":"2010-10-16T00:01:00","slug":"learn-how-to-load-and-use-powershell-snap-ins","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/learn-how-to-load-and-use-powershell-snap-ins\/","title":{"rendered":"Learn How to Load and Use PowerShell Snap-ins"},"content":{"rendered":"<p><strong><\/strong><\/p>\n<p><strong>Summary<\/strong>: How do I use snap-ins with the Windows PowerShell 2.0 cmdlet, <strong>Add-Module<\/strong>? What are the benefits of using this method over <strong>Add-PSSnapin<\/strong>?<\/p>\n<p>&nbsp;<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. I am on holiday and am speaking at <a href=\"http:\/\/www.sqlsaturday.com\/49\/eventhome.aspx\">SQL Saturday #49 today in Orlando, Florida<\/a>. The really cool thing about this particular SQL Saturday is that they have an entire track devoted to Windows PowerShell. This means you could come and spend the whole day learning about Windows PowerShell&mdash;way cool! Luckily, today we have Tome Tanasovski as our guest blogger, and Tome will discuss using Windows PowerShell modules.<\/p>\n<p>Tome is a Windows engineer for a market leading global financial services firm in New York City. He is the founder and leader of the New York City PowerShell User Group, a blogger, and a regular contributor to Microsoft&#8217;s Windows PowerShell forum. He is currently working on the Powershell Bible, which is due in June 2011 from Wiley.<\/p>\n<p>Take it away Tome!<\/p>\n<p>&nbsp;<\/p>\n<p>When I was a child, I knew where the sugar cubes were hidden. I knew that if I opened the left cupboard above the sink, reached my arm to the very back corner, and then moved my hand exactly three jars of tea to the right, I would find the box that held the most precious jewels of dissolving sweetness a five-year-old boy could ever want. Unfortunately, there were physical limitations in the way that prevented me from reaching this treasure trove of candy whenever I desired; my legs and arms were just too short. During that time in my life, my only option was to ask my mother if I could have one sugar cube, and fortunately for me on occasion, she would say yes.<\/p>\n<p>Windows PowerShell 1.0 also had a treasure trove of &ldquo;sugary&rdquo; snap-ins. Unfortunately, in order to use a snap-in, you must also ask your &ldquo;mother&rdquo; (an administrator) to put the snap-in on your computer. I am sure the rest of you share my desire to be allowed to corrupt your pristine teeth and Windows PowerShell session without interference from mommy. Fortunately, just as my arms and legs would eventually grow so that I could reach the cupboard, Windows PowerShell has also matured with the release of version 2.0. You no longer need to register a snap-in&rsquo;s DLL before you can use it. The trick is to treat the snap-in as a binary module. In order to do this you point the <strong>Import-Module<\/strong> cmdlet to the DLL of your favorite snap-in:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\">Import-Module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">c:\\snapins\\ScriptingGuy\\ScriptingGuy.dll<\/span> <\/div>\n<\/blockquote>\n<p>This will work as expected in most scenarios. Let&rsquo;s explore the exceptions so that we can understand why they do not work with the above method. First, let&rsquo;s look at the Quest AD cmdlets. If you run the following from the installation directory of the Quest snap-in, it will appear to load correctly:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\">Import-Module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">.\\Quest.ActiveRoles.ArsPowerShellSnapIn.dll<\/span> <\/div>\n<\/blockquote>\n<p>However, when we start to use the cmdlets, we begin to see a difference between the output that is returned via the module method&hellip;<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0184.WES-10-16-10-01_7FE0ED6B.jpg\"><img decoding=\"async\" height=\"169\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7127.WES-10-16-10-01_thumb_645BEB68.jpg\" alt=\"Image of output returned by module method\" border=\"0\" title=\"Image of output returned by module method\" style=\"border-bottom: 0px;border-left: 0px;margin:;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>&hellip;and the output returned via the snap-in method.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3326.WES-10-16-10-02_6DD4A9DE.jpg\"><img decoding=\"async\" height=\"109\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5468.WES-10-16-10-02_thumb_32A0CE13.jpg\" alt=\"Image of output returned by snap-in method\" border=\"0\" title=\"Image of output returned by snap-in method\" style=\"border-bottom: 0px;border-left: 0px;margin:;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>In the case of the Quest tools, there is a Windows PowerShell format file in the directory called Quest.ActiveRoles.ADManagement.Format.ps1xml that contains a different view for the object that is returned by <strong>Connect-QADService<\/strong>. You can use <strong>Update-FormatData<\/strong> to load this format file into your session, but it is my opinion that this makes loading a snap-in via <strong>Import-Module<\/strong> a bit more cumbersome than just loading it with <strong>Add-PSSnapin<\/strong>. If your only goal is to avoid having administrator privileges, you could consider it unavoidable. Fortunately for us, there is a much more elegant solution to the problem.<\/p>\n<p>Before we look at the solution to the format problem, I&rsquo;d like to look at another problem that can occur when using <strong>Import-Module<\/strong> to load a snap-in. Let&rsquo;s take a look at another extremely popular snap-in that does not come from Microsoft: VMWare&rsquo;s PowerCLI.<\/p>\n<p>We will be using the 4.0 U1 version of PowerCLI because I have not yet installed 4.1. When we load PowerCLI with <strong>Import-Module<\/strong> from the directory PowerCLI was installed into, it appears to load without problem:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\">Import-Module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">VMWare.VimAutomation.dll<\/span> <\/div>\n<\/blockquote>\n<p>However, as soon as we start to use the cmdlets, we immediately see that not everything has been loaded properly. This is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3326.WES-10-16-10-03_6A06DF41.jpg\"><img decoding=\"async\" height=\"89\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6557.WES-10-16-10-03_thumb_0798BA41.jpg\" alt=\"Image of not everything being loaded properly\" border=\"0\" title=\"Image of not everything being loaded properly\" style=\"border-bottom: 0px;border-left: 0px;margin:;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>In this case, there are DLLs in the PowerCLI directory that also need to get loaded before you can use this snap-in as a binary module. Though you could load each assembly manually, this soon becomes cumbersome.<\/p>\n<p>The solution? PSD1 files!<\/p>\n<p>Windows PowerShell 2.0 gives us a new file called a PowerShell Definition file (PSD1&mdash;also known as a module manifest file). This file type can be used to describe and load all of the appropriate pieces you need for a module. PSD1 files have a <strong>FormatsToProcess<\/strong> section in which you can feed it the name of your ps1xml file, a section for <strong>RequiredAssemblies<\/strong> where you can enter all of the DLLs you will require, and even a section called <strong>TypesToProcess<\/strong> which can load custom types from a ps1xml file.<\/p>\n<p>You can take an existing PSD1 file and modify it to suit your needs. For example, to point the manifest file to your snap-in DLL, add two additional DLLs, and then add a format file, you would make sure the following lines are in your module manifest:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\">ModuleToProcess<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8216;ScriptingGuy.snapin.dll&#8217; <\/p>\n<p><\/span><span style=\"color: #000000\">RequiredAssemblies<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8216;ScriptingGuy1.dll&#8217;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\"> &#8216;ScriptingGuy2.dll&#8217; <\/p>\n<p><\/span><span style=\"color: #000000\">FormatsToProcess<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8216;ScriptingGuy.formats.ps1xml&#8217; <br \/><\/span><\/div>\n<\/blockquote>\n<p>You will also need to update the GUID property with a valid unique GUID. Fortunately, there&rsquo;s a much easier way to generate a module manifest file that will do this for you. You can use the <strong>New-ModuleManifest<\/strong> cmdlet that comes with Windows PowerShell 2.0:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\">New-ModuleManifest<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ScriptingGuy.psd1<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Author<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ScriptingGuy<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-CompanyName<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Microsoft<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-ModuleVersion<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Description<\/span><span style=\"color: #808080\"> &#8220;The greatest set of cmdlets ever&#8221; <\/span><span style=\"color: #000000\">-FormatsToProcess<\/span><span style=\"color: #808080\"> &#8220;ScriptingGuy.formats.ps1xml&#8221; <\/span><span style=\"color: #000000\">-RequiredAssemblies<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">@(<\/span><span style=\"color: #808080\">&#8220;ScriptingGuy1.dll&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&#8220;ScriptingGuy2.dll&#8221;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-NestedModules<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">@()<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Copyright<\/span><span style=\"color: #808080\"> &#8220;&#8221; <\/span><span style=\"color: #000000\">-ModuleToProcess<\/span><span style=\"color: #808080\"> &#8221;<\/span><span style=\"color: #000000\">ScriptingGuy.snapin.dll<\/span><span style=\"color: #808080\">&#8221; -TypesToProcess @() -FileList @() <\/span><\/div>\n<\/blockquote>\n<p>Now you can use the following to load your module:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\">Import-Module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">c:\\pathtosnapin\\ScriptingGuy.psd1<\/span> <\/div>\n<\/blockquote>\n<p>There is one final thing you can do to make your life even easier. If you create a folder within your modules directory that has the same name as your psd1 file, you can copy all of your files into your directory so that you can use <strong>Get-Module -ListAvailable<\/strong> and <strong>Import-Module modulename<\/strong> as you do with other modules you may have on your computer.<\/p>\n<p>The technique described above is a perfect way to add third-party snap-ins to your corporate module repository. In addition to allowing users to use snap-ins without requiring administrator privileges, a module manifest can also help you control what is provided to your users. For more information about the fields you can use in a manifest file, you should read through the <strong>get-help -full<\/strong> for <strong>New-ModuleManifest<\/strong> as well as the documentation on MSDN: <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd878297(VS.85).aspx\">How to Write a Module Manifest<\/a>.<\/p>\n<blockquote>\n<p><strong>Note: <\/strong>PowerCLI will still require administrator privileges in order to install VMWare VIX. This is only needed if you wish to use the cmdlets that interface with VMWare VIX like Invoke-VMScript.<\/p>\n<p>&nbsp;<\/p>\n<\/blockquote>\n<p><strong><\/strong><\/p>\n<p>Thank you Tome for being our guest blogger and sharing your knowledge. Tomorrow is guest blogger Oliver Lipkau who will tell us about working with cross forest group memebers.<\/p>\n<p>We invite you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email 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>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: How do I use snap-ins with the Windows PowerShell 2.0 cmdlet, Add-Module? What are the benefits of using this method over Add-PSSnapin? &nbsp; Microsoft Scripting Guy Ed Wilson here. I am on holiday and am speaking at SQL Saturday #49 today in Orlando, Florida. The really cool thing about this particular SQL Saturday is [&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,56,52,3,4,58,61,45],"class_list":["post-16801","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-guest-blogger","tag-modules","tag-scripting-guy","tag-scripting-techniques","tag-tome-tanasovski","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: How do I use snap-ins with the Windows PowerShell 2.0 cmdlet, Add-Module? What are the benefits of using this method over Add-PSSnapin? &nbsp; Microsoft Scripting Guy Ed Wilson here. I am on holiday and am speaking at SQL Saturday #49 today in Orlando, Florida. The really cool thing about this particular SQL Saturday is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16801","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=16801"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16801\/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=16801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=16801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=16801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}