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

后端 未结 23 1546
既然无缘
既然无缘 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:25

    ..

    @ECHO OFF
    SETLOCAL EnableDelayedExpansion EnableExtensions
    NET SESSION >nul 2>&1
    IF %ERRORLEVEL% NEQ 0 GOTO ELEVATE
    GOTO :EOF
    
    :ELEVATE
    SET this="%CD%"
    SET this=!this:\=\\!
    
    MSHTA "javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('CMD', '/K CD /D \"!this!\"', '', 'runas', 1);close();"
    EXIT 1
    

    save this script as "god.cmd" in your system32 or whatever your path is directing to....

    if u open a cmd in e:\mypictures\ and type god it will ask you for credentials and put you back to that same place as the administrator...

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

    Press the Windows + X key and you can now select the Powershell or Command Prompt with admin rights. Works if you are the admin. The function can be unusable if the system is not yours.

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

    While both solutions provided by Dheeraj Bhaskar work, unfortunately they will result in the UAC dialog showing up on top (z-order-wise) but not getting focused (the focused window is the caller cmd/powershell window), thus I either need to grab the mouse and click "yes", or to select the UAC window using Alt+Shift+Tab. (Tested on Win10x64 v1607 build14393.447; UAC = "[...] do not dim [...]".)

    The following solution is a bit awkward as it uses two files, but it preserves the correct focus order, so no extra mouse / keyboard actions are required (besides confirming the UAC dialog: Alt+Y).

    1. cmdadm.lnk (shortcut properties / Advanced... / Run as administrator = ON) %SystemRoot%\System32\cmd.exe /k "cd /d"
    2. su.bat @start cmdadm.lnk %cd%

    Run with su.

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

    Similar to some of the other solutions above, I created an elevate batch file which runs an elevated PowerShell window, bypassing the execution policy to enable running everything from simple commands to batch files to complex PowerShell scripts. I recommend sticking it in your C:\Windows\System32 folder for ease of use.

    The original elevate command executes its task, captures the output, closes the spawned PowerShell window and then returns, writing out the captured output to the original window.

    I created two variants, elevatep and elevatex, which respectively pause and keep the PowerShell window open for more work.

    https://github.com/jt-github/elevate

    And in case my link ever dies, here's the code for the original elevate batch file:

    @Echo Off
    REM Executes a command in an elevated PowerShell window and captures/displays output
    REM Note that any file paths must be fully qualified!
    
    REM Example: elevate myAdminCommand -myArg1 -myArg2 someValue
    
    if "%1"=="" (
        REM If no command is passed, simply open an elevated PowerShell window.
        PowerShell -Command "& {Start-Process PowerShell.exe -Wait -Verb RunAs}"
    ) ELSE (
        REM Copy command+arguments (passed as a parameter) into a ps1 file
        REM Start PowerShell with Elevated access (prompting UAC confirmation)
        REM     and run the ps1 file
        REM     then close elevated window when finished
        REM Output captured results
    
        IF EXIST %temp%\trans.txt del %temp%\trans.txt
        Echo %* ^> %temp%\trans.txt *^>^&1 > %temp%\tmp.ps1
        Echo $error[0] ^| Add-Content %temp%\trans.txt -Encoding Default >> %temp%\tmp.ps1
        PowerShell -Command "& {Start-Process PowerShell.exe -Wait -ArgumentList '-ExecutionPolicy Bypass -File ""%temp%\tmp.ps1""' -Verb RunAs}"
        Type %temp%\trans.txt
    )
    
    0 讨论(0)
  • 2020-12-04 06:32

    I did it easily by using this following command in cmd

    runas /netonly /user:Administrator\Administrator cmd

    after typing this command, you have to enter your Administrator password(if you don't know your Administrator password leave it blank and press Enter or type something, worked for me)..

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

    I use nirsoft programs (eg nircmdc) and sysinternals (eg psexec) all the time. They are very helpful.

    But if you don't want to, or can't, dl a 3rd party program, here's another way, pure Windows.

    Short answer: you can while elevated create a scheduled task with elevated privileges which you can then invoke later while not elevated.

    Middle-length answer: while elevated create task with (but I prefer task scheduler GUI):

    schtasks /create /sc once /tn cmd_elev /tr cmd /rl highest /st 00:00
    

    Then later, no elevation needed, invoke with

    schtasks /run /tn cmd_elev
    

    Long answer: There's a lot of fidgety details; see my blog entry "Start program WITHOUT UAC, useful at system start and in batch files (use task scheduler)"

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