0

Starting from a non-elevated PowerShell, I want to be able to launch a command prompt as Administrator and then run a particular tool as Administrator, ultimately to be used for a right-click menu option.

I have tried this so far:

Start-Process cmd -ArgumentList '/k tool.exe' -Verb RunAs

While this does launch a command prompt as Administrator, it runs tool.exe first in a non-elevated command prompt and then switches to an Administrator command prompt. How can I get this to run tool.exe as Administrator?

Edit: As @mklement0 pointed out, the command prompt actually was being run as Administrator, it just wasn't reflected immediately in the window title (possibly a bug?). Thus, the sample above does actually work as expected.

anosmia
  • 5
  • 5
  • Does this answer your question? [windows core run command with elevated privileges](https://stackoverflow.com/questions/56199624/windows-core-run-command-with-elevated-privileges) – iRon Nov 16 '19 at 07:55
  • @iRon, are you referring to the part that mentions Enter-AdminPSSession.ps1? That seems like it may work, but I was hoping to find a simple solution using Start-Process. If we can confirm that's not possible, I can look into other options. That part that I find confusing is that despite being invoked as Administrator in my example, the command prompt somehow runs tool.exe as non-Administrator first, which doesn't seem like the expected behavior, so I thought perhaps I was doing something wrong. – anosmia Nov 17 '19 at 04:31
  • 1
    @mklement0 After much investigation into the particulars of how I'm using this, I think you're right, it actually was running with elevation all along. There was a bit of a coincidence in that I was getting the same error I get when running tool.exe without elevation, but in fact it was being caused by something else, and the fact that the command prompt window title didn't immediately reflect the Administrator elevation threw me off. Thank you for helping me get to the bottom of this! I have accepted your answer below. – anosmia Nov 19 '19 at 02:40

1 Answers1

0

I think the premise of your question isn't correct, and you may have come to the conclusion in your question due to the fact that the window title of the newly opened window is only updated to reflect administrator status a short while after the window opens.

However, the process associated with the window is elevated from the very beginning, as the following command demonstrates:

# Opens an elevated cmd session in a new window and executes
# `net session`, which only succeeds with elevation.
Start-Process cmd -ArgumentList '/k net session' -Verb RunAs

The net session command succeeds, implying that the process is running with elevation (irrespective of how long it takes the window title to reflect that fact).

mklement0
  • 382,024
  • 64
  • 607
  • 775