PowerShell Script Context Menu Entry to Run Script as Administrator Without Exiting

2

I'm really close on this one. Basically, I want to create a context menu entry for .ps1 files that run the script as administrator without exiting the console window after running. So I can create the key:

Computer\HKEY_CURRENT_USER\SOFTWARE\Classes\SystemFileAssociations\.ps1\shell\RunasAdministratorNoExit

with the values:

enter image description here

and that's all fine and dandy to create the entry. However, in order to actually make it do something, I need to add another key, the command key:

Computer\HKEY_CURRENT_USER\SOFTWARE\Classes\SystemFileAssociations\.ps1\shell\RunasAdministratorNoExit\command

And in this key, I have something that almost works, but not quite. So the default value of the above command key is:

Powershell.exe -WindowStyle Hidden -Command "& {Start-Process PowerShell.exe -Verb RunAs -ArgumentList '-NoLogo -NoExit -File """%1"""'}"

But there's one big problem with this method; when the script finishes, it returns me to the "C:\Windows\system32" directory instead of the directory of the script itself. See my image below for clarification:

enter image description here Further, when I use the context menu entry I created to run scripts containing lines such as:

$settingsfiles = Get-ChildItem -Recurse $scanPath

Instead of it recursing through all the folders local to the script I just ran, it tries to recurse through all the folders at "C:\Windows\system32!" Clearly this is undesired. So here's my question:

What do I need to change in the default value of the command key in order to get the PowerShell script to act as if it was run from the same directory as the script?

jippyjoe4

Posted 2018-07-19T01:45:42.467

Reputation: 630

Did you finally make it? If so, could you please share the code? – JinSnow – 2019-07-19T03:47:14.580

Answers

1

Try leveraging the... Set-Location or Push-Location cmdlet, or the scritpblock …

Split-Path $script:MyInvocation.MyCommand.Path

... in your final command.

Or just create a new shortcut, configured the way you wish and use Right-Click SendTo menu, no reg hacking required.

See this thread showing the SendTo approach for PowerShell.

PowerShell Limit Keys Accepted By Pause Script

postanote

Posted 2018-07-19T01:45:42.467

Reputation: 1 783

The syntax is the part that's tripping me up, though, with the set-location command. How would I incorporate that into the registry entry while still passing the file to powershell so that the original script runs? I basically need to use -command and -file at the same time, but I'm not sure if that works. Can you give an example? – jippyjoe4 – 2018-07-19T16:41:54.107

Okay, I figured it out after messing around for way too long. The quotes were killing me. This is what the line needed to be:

Powershell.exe -WindowStyle Hidden -Command "& {Start-Process powershell.exe -ArgumentList '-nologo -noexit -command ""& {set-location (split-path ''"""%1"""'' -parent) ; & ''"""%1"""''}""' -verb runAs}" – jippyjoe4 – 2018-07-19T18:38:39.673

Glad it all worked out. Yeppers, the quoting can be a PTA. Yet, a quick C&P in of your line into the ISE or VSCode, still shows syntax errors. You know, that whole color coding thing screaming back from the editor. Everything from this [%1"""'' -parent) ; & ''"""%1"""''}""' -verb runAs}"] is shows the wrong colors (purple vs crimson red) to be in the command block. So, a bit strange the it is working as is. Yet, if it is, that's all that matters. I'll have to play with it to tell for sure. – postanote – 2018-07-19T20:56:54.807