Opening up Windows Terminal with elevated privileges, from within Windows Terminal

前端 未结 5 698
旧时难觅i
旧时难觅i 2021-01-05 09:30

There are plenty of questions here which asks how to elevate priviliges from powershell, and almost all of them recommend this command:

Start-Process -Verb Ru         


        
相关标签:
5条回答
  • 2021-01-05 09:59

    Currently you cannot open an elevated wt.exe session from the command line without workarounds. Workarounds include using gsudo, Using Task Scheduler (I tested this one and it works but you need to use the full path to wt.exe and you can skip the shortcut creation step) OR if you are ok with a keyboard shortcut, the simplest way; using a keyboard shortcut to run Windows Terminal as Admin from the taskbar.

    For your use case:

    For my specific instance, I simply want to make it simpler to pop open an admin terminal, I don't need a way to elevate arbitrary commands, then I will happily use the commands I have already shown here.

    The simplest approach will work:

    Pin Windows Terminal as the first item on the task bar. Then hit Win+Ctrl+Shift+1 to open it as admin.

    If you really must launch Windows Terminal from the command line from within Windows Terminal then create a task in the Task Scheduler:

    1. Give the task a name, check 'Run with highest privileges'.
    2. Choose the 'Actions' tab, click 'New', select 'Start a program' as the action. Put the full path to wt.exe in the 'Program/script field'. Click OK. Click OK again.
    3. Click 'Conditions' tab, uncheck "Start the task only if the computer is on AC power".
    4. Click 'Settings' tab, make sure "Allow task to be run on demand" is checked and uncheck "Stop the task if running for longer than".
    5. Finally in your shell (Powershell), launch an elevated Windows Terminal session by running the command: schtasks /run /TN "TASK_NAME" where TASK_NAME is the name you gave the task in step 1.
    0 讨论(0)
  • 2021-01-05 09:59

    In my particular case I also need Windows Terminal opened as administrator all the time. This is what I did, run "where wt" to display the path where Windows Terminal application exe is located, it should be C:\Users\YOURUSER\AppData\Local\Microsoft\WindowsApps\wt.exe. I created a shortcut to that file and checked "Run as administrator" in the advanced properties, then I just pinned it to start and voila. You can delete your temporary shortcut after that if you want.

    0 讨论(0)
  • 2021-01-05 10:03

    Try this:

    powershell "Start-Process -Verb RunAs cmd.exe '/c start wt.exe'"
    

    Also check out these links:

    WT.exe command line arguments: https://docs.microsoft.com/en-us/windows/terminal/command-line-arguments?tabs=windows

    Article about adding Open Windows Terminal Command Prompt to the context menu in Explorer (includes Admin): https://dkcool.tailnet.net/2020/07/add-open-windows-terminal-command-prompt-to-the-explorer-context-menu-in-windows-10/

    Article about adding Open Admin Command Prompt to the context menu in Explorer: https://dkcool.tailnet.net/2019/05/add-open-admin-command-prompt-to-the-explorer-context-menu-in-windows-10/

    0 讨论(0)
  • 2021-01-05 10:07

    Not a direct answer but another option if you have PowerToys is to:

    1. Alt + Space, type Terminal,
    2. Select Run as Administrator (or Ctrl + Shift + Enter)

    You can install PowerToyrs using WinGet

    0 讨论(0)
  • 2021-01-05 10:15

    You can create a shortcut to always run Windows Terminal as administrator using this powershell script:

    $WshShell = New-Object -comObject WScript.Shell
    $Shortcut = $WshShell.CreateShortcut("$Home\Desktop\Windows Terminal.lnk")
    $Shortcut.TargetPath = "$env:LOCALAPPDATA\Microsoft\WindowsApps\Microsoft.WindowsTerminal_8wekyb3d8bbwe\wt.exe"
    $Shortcut.Save()
    
    $bytes = [System.IO.File]::ReadAllBytes("$Home\Desktop\Windows Terminal.lnk")
    $bytes[0x15] = $bytes[0x15] -bor 0x20 #set byte 21 (0x15) bit 6 (0x20) ON 
    [System.IO.File]::WriteAllBytes("$Home\Desktop\Windows Terminal.lnk", $bytes)
    

    You can just paste it and run it from Windows Powershell ISE, it will create a Windows Terminal.lnk file on your desktop. Whenever you double click on that shortcut Windows terminal will run as an admnnistrator

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