4

I want to write a C# method like

public bool PowershellExists()
{
    // Returns true if PowerShell exists on the machine, else false.
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mohammad Nadeem
  • 9,134
  • 14
  • 56
  • 82

1 Answers1

9

Using the MSDN blog post Detection logic for PowerShell installation, I have written the method like:

public bool PowershellExists()
{
    string regval = Microsoft.Win32.Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1", "Install", null).ToString();
    if (regval.Equals("1"))
        return true;
    else
        return false;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mohammad Nadeem
  • 9,134
  • 14
  • 56
  • 82
  • can it also tell the Version ? or can we modify it to somehow get the Version Info ? – N.K Jul 13 '17 at 07:05
  • @N.K if you follow the link there is another key that let you know the version : HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine - in more recent version, you can look in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine – Matthieu Oct 12 '18 at 20:35