How to run PowerShell with admin right

365 timviec 45 Reputation points
2023-03-27T04:15:40.04+00:00

Hi, I need to run PowerShell with admin rights to get permission to run a script in C# code. If there is a solution please leave a comment, many thanks.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,356 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,219 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,040 questions
{count} votes

Accepted answer
  1. hafsa ashraf 80 Reputation points
    2023-03-27T07:15:24.17+00:00

    Here's a possible solution to run PowerShell with admin rights in C# code:

    1. Open Visual Studio and create a new C# console application.
    2. Add a reference to the System.Management.Automation.dll assembly.
    3. Use the following code to launch PowerShell as an administrator:
    using System;
    using System.Management.Automation;
    using System.Security.Principal;
    
    namespace RunPowerShellAsAdmin
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Check if the current user is an administrator
                WindowsIdentity identity = WindowsIdentity.GetCurrent();
                WindowsPrincipal principal = new WindowsPrincipal(identity);
                bool isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
    
                if (!isAdmin)
                {
                    // Relaunch the program as an administrator
                    string[] arguments = Environment.GetCommandLineArgs();
                    string argumentsLine = string.Join(" ", arguments, 1, arguments.Length - 1);
                    string executablePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
                    ProcessStartInfo psi = new ProcessStartInfo(executablePath, argumentsLine);
                    psi.Verb = "runas";
                    Process.Start(psi);
                    return;
                }
    
                // Run PowerShell script as administrator
                using (PowerShell ps = PowerShell.Create())
                {
                    ps.AddScript(@"Start-Process powershell.exe -Verb RunAs -ArgumentList '-File C:\path\to\script.ps1'");
                    ps.Invoke();
                }
    
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
            }
        }
    }
    
    

    This code first checks if the current user is an administrator. If not, it relaunches the program with administrator privileges. Then it launches PowerShell as an administrator and runs the specified script.

    Make sure to replace C:\path\to\script.ps1 with the actual path to your PowerShell script. Also, note that running code as an administrator can be risky and should be used with caution.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2023-03-27T08:52:56.3633333+00:00

    @365 timviec, Welcome to Microsoft Q&A, for clickonce app, you could not run ClickOnce application with Administrative privileges. You could refer to the question to know more about it.

    However, if you just want to run powershell command as admin, you could try to use the similar following command in your c# code:

    Start-Process -FilePath "powershell" -Verb RunAs
    
    

    The above command is used to start another PowerShell process as administrator.


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.