How can I start the PowerShell with admin rights from C#?

185 views Asked by At

my intention is to start a Powershell process with admin rights to pass it arguments in C#. For that I have written following piece of code

public void PassCommand(string command)
    {
        Process process = new Process();
        process.StartInfo.FileName = "powershell.exe";
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.RedirectStandardInput = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.Verb = "runas";
        process.Start();
        process.StandardInput.WriteLine(command);
        process.StandardInput.Flush();
        process.StandardInput.Close();
        Console.WriteLine(process.StandardOutput.ReadToEnd());
    }

I have read in some other articles thatI just have to add this line process.StartInfo.Verb = "runas";. But unfortunately it does not work. The Powershell starts without admin rights. Can someone help me to get this solved?

0

There are 0 answers