How to open an elevated cmd using command line for Windows?

后端 未结 23 1547
既然无缘
既然无缘 2020-12-04 06:21

How do I open a elevated command prompt using command lines on a normal cmd?

For example, I use runas /username:admin cmd but the cmd that was opened do

相关标签:
23条回答
  • 2020-12-04 06:51

    I don't have enough reputation to add a comment to the top answer, but with the power of aliases you can get away with just typing the following:

    powershell "start cmd -v runAs"
    

    This is just a shorter version of user3018703 excellent solution:

    powershell -Command "Start-Process cmd -Verb RunAs"
    
    0 讨论(0)
  • 2020-12-04 06:51

    Simple way I did after trying other answers here

    Method 1: WITHOUT a 3rd party program (I used this)

    1. Create a file called sudo.bat (you can replace sudo with any name you want) with following content powershell.exe -Command "Start-Process cmd \"/k cd /d %cd%\" -Verb RunAs"
    2. Move sudo.bat to a folder in your PATH; if you don't know what that means, just move these files to c:\windows\
    3. Now sudo will work in Run dialog (win+r) or in explorer address bar (this is the best part :))

    Method 2: WITH a 3rd party program

    1. Download NirCmd and unzip it.
    2. Create a file called sudo.bat (you can replace sudo with any name you want) with following content nircmdc elevate cmd /k "cd /d %cd%"
    3. Move nircmdc.exe and sudo.bat to a folder in your PATH; if you don't know what that means, just move these files to c:\windows\
    4. Now sudo will work in Run dialog (win+r) or in explorer address bar (this is the best part :))
    0 讨论(0)
  • 2020-12-04 06:51

    Make the batch file save the credentials of the actual administrator account by using the /savecred switch. This will prompt for credentials the first time and then store the encrypted password in credential manager. Then for all subsequent times the batch runs it will run as the full admin but not prompt for credentials because they are stored encrypted in credential manager and the end user is unable to get the password. The following should open an elevated CMD with full administrator privileges and will only prompt for password the first time:

    START c:\Windows\System32\runas.exe /user:Administrator /savecred cmd.exe
    
    0 讨论(0)
  • 2020-12-04 06:51

    I've been using Elevate for awhile now.

    It's description - This utility executes a command with UAC privilege elevation. This is useful for working inside command prompts or with batch files.

    I copy the bin.x86-64\elevate.exe from the .zip into C:\Program Files\elevate and add that path to my PATH.

    Then GitBash I can run something like elevate sc stop W3SVC to turn off the IIS service.

    Running the command gives me the UAC dialog, properly focused with keyboard control and upon accepting the dialog I return to my shell.

    0 讨论(0)
  • 2020-12-04 06:52

    There are several ways to open an elevated cmd, but only your method works from the standard command prompt. You just need to put user not username:

    runas /user:machinename\adminuser cmd
    

    See relevant help from Microsoft community.

    0 讨论(0)
提交回复
热议问题