{"id":10521,"date":"2006-04-25T12:18:32","date_gmt":"2006-04-25T12:18:32","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/04\/25\/the-worlds-easiest-way-to-createinstall-mshsnapins\/"},"modified":"2019-02-18T13:24:53","modified_gmt":"2019-02-18T20:24:53","slug":"the-worlds-easiest-way-to-createinstall-mshsnapins","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/the-worlds-easiest-way-to-createinstall-mshsnapins\/","title":{"rendered":"The worlds easiest way to create\/install MshSnapins"},"content":{"rendered":"<p>Hopefully you&#8217;ve read the previous entries on how to create MshSnapins to include your own cmdlets\/providers without having to create an entire separate executable (custom shell).<\/p>\n<p>If you didn&#8217;t or if you did and you don&#8217;t feel like writing the extra MshSnapin class to be used by InstallUtil.exe, you&#8217;re in luck. I wrote a little ditty(script) about taking any arbitrary assembly and setting the appropriate values in the Registry for use.And in using this little handy script, you&#8217;ll be able to look at registry settings to see what needs to get added to add your mshsnapin. <\/p>\n<p>This script takes 2 parameters. first parameter is the assembly containing your cmdlets and\/or providers. The 2nd optional parameter is the &#8220;name&#8221; of the mshsnapin. If you don&#8217;t supply a name, then the name of the mshsnapin will be the assemblyfile (1st parameter)<br \/>Lets assume that you created an assembly &#8220;myawesomecmdlets.dll&#8221; which contain the most awe-inspiring cmdlets possibly imaginable.<\/p>\n<p>MSH&gt; register-mshsnapin myawesomecmdlets.dll mysnapin<br \/>MSH&gt; add-mshsnapin&nbsp; mysnapin&nbsp; # add your snapin to the current session<br \/>MSH&gt;&nbsp;create-somethingwonderful # run one of your cmdlets \ud83d\ude42<\/p>\n<p>If you run regedit, you&#8217;ll then be able to see the entries that get added&nbsp;for your mshsnapin &#8220;mysnapin&#8221;: <br \/>HKLM\\Software\\Microsoft\\MSH\\1\\MshSnapins\\mysnapin has values for<br \/>ApplicationBase &#8211; directoryname where your assembly is located. This is also the dir where help files, types &amp; format files are looked for<br \/>AssemblyName &#8211; Full strong name of assembly<br \/>Description &#8211; text describing your mshsnapin<br \/>ModuleName &#8211; full path of assembly file<br \/>MshVersion &#8211; &#8220;1.0&#8221;<br \/>Version &#8211; &#8220;1.0&#8221;<\/p>\n<p>&nbsp;<\/p>\n<p>So use the script at end of this post to quickly take any assembly and configure it be added as an mshsnapin. <\/p>\n<p>Hope this helps!<\/p>\n<p>Scott<\/p>\n<p>&nbsp;<\/p>\n<p># Beginning of script &#8216;register-mshsnapin&#8217;<\/p>\n<p>param ([string] $assemblyfile, [string] $MshSnapinName = $assemblyFile)<\/p>\n<p># Make sure we can load assembly<br \/>################################<br \/>$assemblyInfo = [System.Reflection.Assembly]::LoadFrom($assemblyFile)<br \/>if($assemblyInfo -eq $null)<br \/>{<br \/>&nbsp;&nbsp; &#8220;unable to load assembly $assemblyFile!&#8221;<br \/>&nbsp;&nbsp; exit -1<br \/>}<\/p>\n<p># Based on MshSnapinName, lets create the Reg Key values<br \/>#################################################<br \/>$keyname = &#8220;HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Msh\\1\\MshSnapins\\&#8221; + $MshSnapinName<\/p>\n<p>[Microsoft.Win32.Registry]::SetValue($keyname, &#8220;ApplicationBase&#8221;,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [System.IO.Path]::GetDirectoryName($assemblyInfo.Location) )<br \/>[Microsoft.Win32.Registry]::SetValue($keyname, &#8220;AssemblyName&#8221;,$assemblyInfo.FullName)<\/p>\n<p>[Microsoft.Win32.Registry]::SetValue($keyname,&#8221;Description&#8221;,&#8221;A Test MshSnapin&#8221;)<br \/>[Microsoft.Win32.Registry]::SetValue($keyname,&#8221;ModuleName&#8221;,$assemblyInfo.Location)<br \/>[Microsoft.Win32.Registry]::SetValue($keyname,&#8221;Version&#8221;,&#8221;1.0&#8243;)<br \/>[Microsoft.Win32.Registry]::SetValue($keyname,&#8221;MshVersion&#8221;,&#8221;1.0&#8243;)<\/p>\n<p># End of script<\/p>\n<p>[<i>Edit: Monad has now been renamed to Windows PowerShell.  This script or discussion may require slight adjustments before it applies directly to newer builds.<\/i>]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hopefully you&#8217;ve read the previous entries on how to create MshSnapins to include your own cmdlets\/providers without having to create an entire separate executable (custom shell). If you didn&#8217;t or if you did and you don&#8217;t feel like writing the extra MshSnapin class to be used by InstallUtil.exe, you&#8217;re in luck. I wrote a little [&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":[],"class_list":["post-10521","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>Hopefully you&#8217;ve read the previous entries on how to create MshSnapins to include your own cmdlets\/providers without having to create an entire separate executable (custom shell). If you didn&#8217;t or if you did and you don&#8217;t feel like writing the extra MshSnapin class to be used by InstallUtil.exe, you&#8217;re in luck. I wrote a little [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10521","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=10521"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10521\/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=10521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=10521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=10521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}