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

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

    According to documentation, the Windows security model...

    does not grant administrative privileges at all times. Even administrators run under standard privileges when they perform non-administrative tasks that do not require elevated privileges.

    You have the Create this task with administrative privileges option in the Create new task dialog (Task Manager > File > Run new task), but there is no built-in way to effectively elevate privileges using the command line.

    However, there are some third party tools (internally relying on Windows APIs) you can use to elevate privileges from the command line:

    NirCmd:

    1. Download it and unzip it.
    2. nircmdc elevate cmd

    windosu:

    1. Install it: npm install -g windosu (requires node.js installed)
    2. sudo cmd
    0 讨论(0)
  • 2020-12-04 06:45

    The following as a batch file will open an elevated command prompt with the path set to the same directory as the one from where the batch file was invoked

    set OLDDIR=%CD%
    powershell -Command "Start-Process cmd -ArgumentList '/K cd %OLDDIR%' -Verb RunAs "
    
    0 讨论(0)
  • 2020-12-04 06:46

    Here is a way to integrate with explorer. It will popup a extra menu item when you right-click in any folder within Windows Explorer:

    Here are the steps:

    1. Create this key: \HKEY_CLASSES_ROOT\Folder\shell\dosherewithadmin
    2. Change its Default value to whatever you want to appear as the menu item text. Ex "DOS Shell as Admin"
    3. Create this another key: \HKEY_CLASSES_ROOT\Folder\shell\dosherewithadmin\command
    4. Change its default value to this, ipsis litteris: powershell.exe -Command "Start-Process -Verb RunAs 'cmd.exe' -Args '/k pushd "%1"'"
    5. It's done. Now right-click in any folder and you will see your item there within the other items.

    *Use pushd instead of cd to allow it to work in any drive. :-)

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

    Can use a temporary environment variable to use with an elevated shortcut (

    start.cmd

    setx valueName_betterSpecificForEachCase %~dp0
    "%~dp0ascladm.lnk"
    

    ascladm.lnk (shortcut)

    _ properties\advanced\"run as administrator"=yes
    

    (to make path changes you'll need to temporarily create the env.Variable)

    _ properties\target="%valueName_betterSpecificForEachCase%\ascladm.cmd"
    
    _ properties\"start in"="%valueName_betterSpecificForEachCase%"
    

    ascladm.cmd

    setx valueName_betterSpecificForEachCase=
    reg delete HKEY_CURRENT_USER\Environment /F /V valueName_betterSpecificForEachCase
    "%~dp0fileName_targetedCmd.cmd"
    

    ) (targetedCmd gets executed in elevated cmd window)

    Although it is 3 files ,you can place everything (including targetedCmd) in some subfolder (do not forget to add the folderName to the patches) and rename "start.cmd" to targeted's one name

    For me it looks like most native way of doing this ,whilst cmd doesn't have the needed command

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

    My favorite way of doing this is using PsExec.exe from SysInternals, available at http://technet.microsoft.com/en-us/sysinternals/bb897553

    .\psexec.exe -accepteula -h -u "$username" -p "$password" cmd.exe
    

    The "-h" switch is the one doing the magic:

    -h If the target system is Vista or higher, has the process run with the account's elevated token, if available.

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

    Just use the command: runas /noprofile /user:administrator cmd

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