Reputation: 4251
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.
Upvotes: 1
Views: 1940
Reputation: 201652
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.
Upvotes: 2