How to Run as Administrator in Command Lines on Windows

  • by

Generally, we do not log on Windows as Administrator to do routine jobs, though we are in Administrator group. We usually use a normal user, like steven to finish our daily works. At times, you may need the privilege of Administrator to do something unusual.

For example, in some cases, we need to modify C:\Windows\System32\drivers\etc\hosts for resolving our own domain names in LAN. But this file is read-only for security reasons, which means you can read but you can't write, rename or delete. You need Administrator privilege to finish the task.

You don't have to actually leave current account and log on as Administrator, you can acquire Administrator privilege on demand. Let's do it step by step:

  1. Press ⊞ Win + r to Open a Run Dialog.
  2. Open a Run Dialog

    Open a Run Dialog

    You can see a Run Dialog is opened.

    A Run Dialog

    A Run Dialog

  3. Enter runas command string in the dialog:
  4. runas /user:\administrator "notepad %SystemRoot%\System32\drivers\etc\hosts"

    If we want to run the command in another computer, we should indicate the computer name.

    runas /user:ED-COMPUTER\administrator "notepad %SystemRoot%\System32\drivers\etc\hosts"
    Enter runas command in Run Dialog

    Enter runas command in Run Dialog

    To customize for your own tasks, please replace ED-COMPUTER with your computer name, and type your command inside the pair of double quotes.

    For more examples, you can refer to Microsoft TechNet Library: TN Runas.

  5. Enter the administrator password to let OS authenticate the credentials.
  6. Administrator Authentication

    Administrator Authentication

    For batch files or scripting purposes, you can add an option /savecred in the command line to skip the authentication step if the account had been ever authenticated:

    runas /user:\administrator /savecred "notepad %SystemRoot%\System32\drivers\etc\hosts"
  7. The command is executed as administrator.
  8. Now, you will see the notepad opened as an administrator. You can modify the file and save.

    Notepad Run as Administrator

    Notepad Run as Administrator

This is how we run as Administrator in command lines on Windows. Actually, Run Dialog is not the only place that you can put the runas command string, you can put it in a CMD Dialog also.

Enter runas command in CMD Dialog

Enter runas command in CMD Dialog

Or put it in a Windows Explorer address line.

Enter runas command in Windows Explorer

Enter runas command in Windows Explorer

Leave a Reply

Your email address will not be published. Required fields are marked *