Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open PowerShell as administrator on specific directory from right-click menu

I need a single command that opens a new elevated PowerShell window on a given path. why? I want a shortcut for opening Powershell as admin on given directory(just like holding shift and right-click on any directory will give you, but as admin.)

I used this to add an "open powershell window as admin here" shortcut to my right-click menu.

currently, I have

powershell -Command "Start-Process PowerShell -Verb RunAs"

that will open elevated PowerShell in C:\WINDOWS\system32 , or:

powershell.exe -noexit -command Set-Location -literalPath '%V'

that will open a new not elevated powershell window on current directory

I tried

powershell -Command "Start-Process PowerShell -Verb RunAs; Set-Location -literalPath '%V'"

but it does not navigate to the directory.

note - %v becomes the path inside the directory you right-clicked in it.

How can I combine correctly these commands to open elevated PowerShell in the wanted directory?

like image 441
Eliav Louski Avatar asked Jun 29 '21 16:06

Eliav Louski


People also ask

How do I Run PowerShell as administrator in current directory?

You can do this with the native method by simply clicking in the folder you wish to open Powershell as administrator from File Explorer, and then press Alt F S A one key right after the other. Now PowerShell will open as administrator in that folder as requested.

How to open PowerShell as administrator in Windows 11?

To open PowerShell as Administrator in Windows 11, do the following. Open the Start menu and click the "All Apps" button. Scroll down and find the "Windows Tools" icon. Open Windows Tools folder and find PowerShell. Right-click PowerShell and select Run as Administrator.

How do I open a PowerShell window in a specific directory?

Using ALT+F+S+R to open a regular PowerShell window in current directory. Holding the ALT key while navigating the context menu will display keys to use to jump through menus. The shortcut keys work in Windows 10, Windows Server 2016, and Windows Server 2019.

How do I open a PowerShell menu from the command line?

With the powershellmenu key selected, double-click the (Default) value to open its properties window. In the properties window, set the value in the “Value data” box to “Open PowerShell Here” and then click “OK.”

How do I run a PowerShell profile as an administrator?

Open Terminal by right-clicking the Start button and selecting Windows Terminal (Admin). Click its menu button on the tab strip and select Settings. Or simply press Ctrl + , (comma). Select PowerShell in the left panel. On the right, find the " Run this profile as Administrator " toggle option and enable it. Click Save to apply changes.


1 Answers

Here's Windows registry setting; works on rigt-click from Windows Explorer:

reg query HKEY_CLASSES_ROOT\Directory\shell\runasPowerShell -s

HKEY_CLASSES_ROOT\Directory\shell\runasPowerShell
    HasLUAShield    REG_SZ
    (Default)    REG_SZ    PowerShell here as administrator

HKEY_CLASSES_ROOT\Directory\shell\runasPowerShell\command
    (Default)    REG_SZ    Powershell Start-Process PowerShell -verb runas -ArgumentList '-noexit', 'Push-Location -literalPath ''""""%V""""'''

Note that the tricky quoting ''""""%V""""'' guarantees mistake-free operation even for paths with spaces or other special characters.

like image 114
JosefZ Avatar answered Nov 15 '22 03:11

JosefZ