1

I would like to know if there is a method which I can call which can tell me if a PowerShell snapin is installed.

I know I could call probably do it, say via WMI or write a PowerShell script and return a list to C#, but is there a method to do it somewhere.

Thanks.

1 Answer 1

2

I'm not sure if this is the optimal way to do it but take a look at the default runspace configuration e.g.:

using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

var cmdlets = Runspace.DefaultRunspace.RunspaceConfiguration.Cmdlets;
var snapins = (from cmdlet in cmdlets
              select new { cmdlet.PSSnapin.Name }).Distinct();

Compiled by hand so YMMV.

To see which snapins are installed, instead of loaded, enumerate the contents of this registry key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns

Or you could invoke Get-PSSnapin -Registered from the C# code and process the PSSnapInInfo objects that are returned.

Sign up to request clarification or add additional context in comments.

8 Comments

Hi Keith, I'll test it. Thanks. Bob.
Kieth, Unfortunately that doesn't work. That script, when fixed, only displays the current spanins that are loaded into the runspace when the runspace is opened. Effectively the ones which are part of the powershell install. Thanks for the effort. Bob.
Ah, in that case, just enumerate the contents of the regkey HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns
Hi Kieth, That doesn't work either, as that registry key, is only populated when you have added a windows installer installed snapin into a runspace. I know their is a WMI call which evaluates all windows installer installed products, but if you install a snapin using a 3rd party installer, then WMI will not pick it up. Which leaves a whole. Any other ideas? I think if their is nothing else, there is no simple answer. Bob
Hmm both Get-PSSnapin and this registry key will show you all installed snapins on that machine (hence the location - HKEY_LOCAL_MACHINE). This is irrespective of runspace. BTW when I say installed I don't mean loaded via Add-PSSnapin. I mean that you either ran a setup.exe (or .msi) file to install the snapin or you manually ran installutil.exe on the snapin's DLL. IOW, before you can load a snapin via Add-PSSnapin, the snapin has to be registered with PowerShell (ie has an entry under the registry key shown above).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.