如何使用C#检查PowerShell是否已安装

Moh*_*eem 3 c# powershell

我想写一个像C#的方法

public bool PowershellExists()
{
    // Returns true if PowerShell exists on the machine, else false.
}
Run Code Online (Sandbox Code Playgroud)

Moh*_*eem 7

使用MSDN博客文章检测逻辑进行PowerShell安装,我编写了如下方法:

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;
}
Run Code Online (Sandbox Code Playgroud)