Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 RunAs powershell.exe .....

(or pwsh.exe for Powershell Core)

And this works, it opens up a new Powershell window with elevated privileges, after having gone through a UAC block to verify access.

However, while I'm using Powershell, I'm doing it inside Windows Terminal, the new terminal application for Windows 10, and I would like to open a new instance of Windows Terminal with elevated privileges, not just a Powershell window.

So I tried these commands:

Start-Process -Verb RunAs wt
Start-Process -Verb RunAs wt.exe
Start-Process -Verb RunAs (Get-Command wt)
Start-Process -Verb RunAs "C:\Users\lasse\AppData\Local\Microsoft\WindowsApps\wt.exe"

They all fail with:

Start-Process: This command cannot be run due to the error: The file cannot be accessed by the system..

I'm assuming this has something to do with where the executable is located, within my profile, but if I right-click the Windows Terminal icon I have on my task bar and choose to run it as administrator, it opens up just fine. This is what I want to duplicate.

So is there a way for me to modify either the commands I tried above, or change some access setting that would make this work?

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.

like image 446
Lasse V. Karlsen Avatar asked Jun 21 '20 09:06

Lasse V. Karlsen


People also ask

How do I open elevated PowerShell in Windows Terminal?

You can launch the new elevated PowerShell in any Windows Terminal by clicking the chevron near the top and selecting the PowerShell (Administrator) profile. That's it — you can now have both admin and non-admin PowerShell windows open in the same terminal.

How do I increase privileges in Windows Terminal?

To run Windows Terminal with elevated administrator permission (admin mode), right-click on the Windows Terminal icon, then again on the Windows Terminal title that displays, then select "Run as administrator".

How do you Run with elevated privileges?

When using the Start Menu, hold down Shift+Ctrl when launching an application to launch it 'As Admin' (elevated) this has the same effect as if you right click and select Run as local Administrator. To run an item on the Start menu / Search /Desktop elevated, select it and then press Ctrl+Shift+Enter on the keyboard.

How do I get elevated access in cmd?

In the search box, type cmd and press Ctrl + Shift + Enter . If done correctly, the User Account Control window below opens. Click Yes to run the Windows Command Prompt as Administrator.


3 Answers

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.
like image 84
apena Avatar answered Oct 19 '22 21:10

apena


Try this:

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

Also check out these links:

WT.exe command line arguments: https://learn.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/

like image 17
DK_ Avatar answered Oct 19 '22 20:10

DK_


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 PowerToys using WinGet

like image 15
kramfs Avatar answered Oct 19 '22 22:10

kramfs