How to run PowerShell with admin privileges from current folder?
Asked Answered
I

3

10

How can I run PowerShell from Context menu (holding the shift key) in current folder with admin privileges? Is there a simpler way than type in any code to windows registry?

Interpret answered 14/11, 2016 at 22:58 Comment(2)
That question is IMO off topic in stackoverflow. Depending on your windows version, why not place a quick access icon in explorer title bar?Agathaagathe
If it is offtop, i am sorry for that. I was just searching for answear for few hours, so i decided to share this knowledge with community. I don't really know what you have on Your mind, could You explain Your solution ?Interpret
I
11

The script below can be pasted into any file with reg extension and run. After that, you will be able to run powershell with admin privileges from current open folder. To do this, you have to hold shift and then right click on empty space in the folder.

Code:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin]
@="Open PowerShell window here as administrator"
"Extended"=""
"HasLUAShield"=""
"Icon"="powershell.exe"

[HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin\command]
@="C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -windowstyle hidden -Command \"Start-Process powershell  -ArgumentList '-NoExit', '-Command cd %V' -Verb runAs\""

Context menu view:

enter image description here

Interpret answered 14/11, 2016 at 22:58 Comment(0)
S
16

As LotPings mentions in the comments, you can add a simple shortcut to the Quck Access Toolbar if you are running Windows 10. It's supported by Windows by default.

This image should explain how to set it up. Shortcut powershell-admin

While this doesn't use shift, you still have a quick way of starting PowerShell in the current directory as admin.

Safko answered 15/11, 2016 at 10:24 Comment(1)
Thanks Jonas, had at the time no access to an english Win10.Agathaagathe
I
11

The script below can be pasted into any file with reg extension and run. After that, you will be able to run powershell with admin privileges from current open folder. To do this, you have to hold shift and then right click on empty space in the folder.

Code:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin]
@="Open PowerShell window here as administrator"
"Extended"=""
"HasLUAShield"=""
"Icon"="powershell.exe"

[HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin\command]
@="C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -windowstyle hidden -Command \"Start-Process powershell  -ArgumentList '-NoExit', '-Command cd %V' -Verb runAs\""

Context menu view:

enter image description here

Interpret answered 14/11, 2016 at 22:58 Comment(0)
L
7

Dominik's registry solution works well, but there is a problem when the folder path contains spaces. In the command, you have to add (escaped, i.e. double) apostrophes around the path.

C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -windowstyle hidden -Command "Start-Process powershell -ArgumentList '-NoExit', '-Command cd ''%V''' -Verb runAs"

Liver answered 16/11, 2018 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.