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?
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.
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.
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.
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.”
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With