C# sample to open command prompt for other than currently logged user

.Net frameworks 2.0 have some new cool managed APIs. Here is one sample. It is common need to run a process on a user account other than currently logged on user. Following is the simple handy code which I use to open a command prompt with amin previleges, when I am logged on as normal user. There may be many other usage we could find for this Process.Start() API to make our life easier.

using System;
using System.Security;
using System.Diagnostics;

public class RunAs{
    public static void Main(string[] args) {
        try {
            Console.Write(@"User Name [domain\user] : ");
            string[] userInfo = (Console.ReadLine()).Split('\\'); 
            SecureString secPass = ReadPassword();  

     catch(Exception e){
    public static SecureString ReadPassword()
        SecureString secPass = new SecureString();
        Console.Write("Enter your password : ");
        ConsoleKeyInfo key = Console.ReadKey(true);
        while(key.KeyChar != '\r')
            key = Console.ReadKey(true);
         return secPass;

This posting is provided "AS IS" with no warranties, and confers no rights

Comments (4)

  1. C# sample to open command prompt for other than currently logged user

  2. trodriguez says:

    this code might be useful for other things but for the purpose you describe it sounds kind of dumb because:

    1. It might be easier to right-click the console icon and click "Run as…"; or if you like a command line tool,

    2. A better tool ships with Windows (runas.exe)

    -try runas /user:<DOMAIN><USERNAME> cmd

  3. Mike says:

    You work at Microsoft? This is the dumbest code I’ve ever seen – has no value, waste of your time man.

  4. stopBy says:

    this code works if you have a dos-project.

    sorry you have put up bad code

Skip to main content