42

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.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825

13 Answers13

39

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.
apena
  • 2,091
  • 12
  • 19
  • 6
    The pin-to-taskbar is a great workaround that fits my use-case. Another option if you have PowerToys is to: 1. Alt + Space, type Terminal, 2. Select Run as Administrator (or Ctrl + Shift + Enter) - https://github.com/microsoft/PowerToys - https://github.com/microsoft/winget-cli – kramfs Aug 19 '20 at 07:48
  • 1
    FYI, do NOT browse to wt.exe. It will error when you save, because wt.exe has 0 bytes in it, you get some system error. open a cmd prompt, do where wt.exe, then copy/paste that path in. However for me, all this does is launch a new powershell session outside of windows terminal. and for me, "TASK_NAME" had to be Microsoft\" otherwise I got system not found. – user3622356 Jun 01 '21 at 21:59
  • I installed gsudo (using chocolatey), and prepended my default profile with it: `"commandline": "gsudo powershell.exe"`. A minor side note: Windows Terminal now also has a 'startOnUserLogin' option (or enable in the configuration GUI under 'Startup'). – WizxX20 Dec 02 '21 at 10:13
  • `Win+Ctrl+Shift` then click the icon in taskbar works for me – ch271828n Mar 05 '22 at 06:20
19

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/

DK_
  • 331
  • 1
  • 5
  • 2
    And if you want to use a certain profile, using `powershell "Start-Process -Verb RunAs cmd.exe '/c start wt.exe -p ""<>""'"` works – mu88 Apr 27 '21 at 09:47
14

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

kramfs
  • 1,989
  • 1
  • 13
  • 12
  • 1
    I don't use the powertoys run app as it is severly limited in what it finds on my system, it seem to find only the things that have been directly installed and not all the things available in the path, so I skipped it, but thanks for the suggestion. – Lasse V. Karlsen Aug 19 '20 at 07:56
  • 2
    Interesting, mine it has a fairly good indexed on what's in my system. I saved a cloud-config file somewhere and it was able to find it just by typing `cloud`. I typed ubuntu and it finds the running k3s-master provisioned by Multipass, so I have experience with it. – kramfs Aug 19 '20 at 08:04
  • I have it installed and i must be missing something, i get asked for UAC prompt each time. – IbrarMumtaz Jul 18 '23 at 14:58
14

With recent releases, this issue appears to be fixed. It works now, doing exactly as you originally tried and failed (Start-Process -verb RunAs wt). I would recommend trying again now with the latest releases (at least Windows Terminal, and perhaps PowerShell as well).

No need for workarounds anymore!!

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
fialdrexs
  • 141
  • 1
  • 4
  • Agreed - Whatever fix it was has been in there long enough for it to have worked in at least Windows Terminal 1.4 (the full release before the current release), so no Preview is needed. Or it could be a fix in a PowerShell release. I know I've been doing it that way for at least 6 months, myself. – NotTheDr01ds Feb 18 '21 at 16:19
  • It's also possible it was simply a path issue for the original user (which should have been part of an answer at that time). Perhaps they installed Windows Terminal through the github release, instead of through the Store? – NotTheDr01ds Feb 18 '21 at 16:24
  • 2
    I'm running both Windows Terminal preview v1.6.10412.0 and Windows Terminal stable v1.5.10411.0 , and I still get "The file cannot be accessed by the system," when trying to run "Start-Process -Verb RunAs wt" on either of them. So, I just do my normal process of right-clicking on the pinned taskbar item, right-clicking on "Windows Terminal" from the jump list, and picking "Run as Administrator." It's janky, but it works, so... – Mike Loux Feb 23 '21 at 15:56
  • @MikeLoux: That might be the simplest answer in this thread. Works like a charm. – Simon Elms Jun 24 '21 at 04:48
  • The fact is that you need to run this command as an admin to become an admin in WT. So it does not work if you run it as an unprivileged user from Powershell. To check if you are currently an admin in Powershell, go for this solution https://serverfault.com/questions/95431/in-a-powershell-script-how-can-i-check-if-im-running-with-administrator-privil – t7e Feb 21 '22 at 09:09
5

Windows Terminal has a feature to automatically run as administrator in the preview; no need for workarounds now.

SuperStormer
  • 4,997
  • 5
  • 25
  • 35
Selflo
  • 59
  • 1
  • 1
  • 1
    In settings, click on 'Open JSON file', then add "elevate": true in the 'defaults' section. But (as of 3/22) you need to first install the Preview version of Terminal. I don't always need elevated, so I went with the Power Tools option outlined in another answer. I already had that installed, as I like Power Tools for other things, like subdividing my wide screen monitor in 3 sections. – MattC Mar 12 '22 at 17:08
  • Mine has this setting applied and i am prompted for UAC each time i open it. – IbrarMumtaz Jul 18 '23 at 15:09
3

Pin to the Taskbar and hold Ctrl + Shift while left clicking on the Windows Terminal icon.

omcbubba
  • 57
  • 1
2

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.

Allan Gamboa
  • 131
  • 2
2

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

1

I know this answer does not fully match your question but given that also other answers were oriented in this way I hope this won't disturb the discussion.

I always need to run PowerShell as Administrator and I only want to use Windows Terminal, which given it's restrictions cannot be configured to run always as Administrator.

I hated the need to use shortcuts and other hacks I found being suggested online, so I think I found a better solution but you have to pay the cost of a 1/2 seconds at startup.

  1. Locate your user profile (A profile is a Windows PowerShell ISE script that runs automatically when you start a new session) using _ $PROFILE
  2. Edit profile with any preferred editor _ code $PROFILE
  3. Adde the following code to the profile file and save it
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
    $host.ui.rawui.windowtitle="Bootstrapper"
    Start-Process -Verb RunAs wt
} else {
    $Processes = Get-Process | Where-Object {$_.mainWindowTitle -eq 'Bootstrapper' -and $_.name -eq 'WindowsTerminal'}
    if($Processes.count -gt 0)
    {
        Stop-Process -Id $Processes[0].id
    }
}

What the script do? You can pin Windows Terminal icon to your application bar and when you click there WT will start as non elevated user, but the profile will understand if this is the case. When you are not running as Administrator it will change the name of the window and start a new WT as administrator. The new instance will also execute the profile file and if the instance is runinng as Administrator, it will look for the WT named Bootstrapper and kill it. This proces takes between one and two seconds, I prefer this way other than right clicking on the icon.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Norcino
  • 5,850
  • 6
  • 25
  • 42
0

It's likely you were just facing a Path issue. I know that the command examples you gave (e.g. Start-Process -verb RunAs wt) have worked for me for some time (as mentioned in @fialdrexs's answer).

Did you install Windows Terminal from a Github release or from the Store?

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
0

I currently have the following entry in my settings.json profiles list to add an elevated Windows Terminal to the drop down options:

{
    // https://github.com/microsoft/terminal/issues/632#issuecomment-663686412
    "name": "Windows Terminal (elevated)",
    "commandline": "%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -Command Start-Process -Verb RunAs \"shell:appsFolder\\Microsoft.WindowsTerminal_8wekyb3d8bbwe!App\"",
    "hidden": false,
    "icon": "ms-appx:///Images/Square44x44Logo.targetsize-32.png"
}

The comment with the GitHub link should get you to where I originally found this information.

Michael
  • 497
  • 1
  • 5
  • 7
0

I find a away to workaround, just create a file bat with content

powershell Start-Process -Verb RunAs wt.exe

  1. Save file yourfile.bat to folder you want.
  2. Add path folder to System Environment.
  3. Press WINDOW + R and type file bat name.
Minh Sự
  • 97
  • 2
  • 8
-2

Currently this problem was fixed, but it ended up with a weird issue. Running wt.exe from Win+R, searching it on start menu, and starting itself from the terminal, show the same error message.

The application was unable to start correctly (0xc0000022). Click OK to close the application.

However it works when executing wt via Command Prompt, PowerShell, and PowerShell Core.

So just a quick workaround answer, start PowerShell and run the command, Start-Process -Verb RunAs wt.exe; or the simpler start -verb runas wt

Yuuki Rika
  • 1
  • 1
  • 2
  • 1
    Welcome to Stack Overflow and thanks for your answer. As a new contributor, please make sure you fully read the question and existing answers before posting a new answer. `Start-Process -Verb RunAs wt.exe` was listed in the original question as one of the commands that *didn't work* for the original poster (at the time). [This answer](https://stackoverflow.com/a/66262771/11810933) also mentions that the command should *now* work based on recent releases (but still doesn't for some people, as noted in the comments). I don't see that your answer is any different from that one. – NotTheDr01ds Feb 25 '21 at 14:04