This project is read-only.
1
Vote

Invoke-Elevated doesn't close shell, even when passing exit

description

The invoke-elevated help has this example:
C:\PS>Invoke-Elevated {gci c:\windows\temp | export-clixml tempdir.xml; exit}

    Executes the scriptblock in an elevated PowerShell instance.
I find this cmdlet extremely useful as kind of a sudo for Windows. But my issue is that the spawned powershell never closes after executing the command, even when I pass exit like in the example:
Invoke-Elevated {echo "hi"; exit}
opens a new powershell, displays "hi" and then shows a normal powershell prompt.

comments

r_keith_hill wrote Jan 21, 2016 at 4:16 AM

We actually try hard to keep the window open because it would normally exit on its own. Making it hard to determine whether your command succeeded or failed. I have a fix where you can specify -AutoExit to this command. So you would invoke like so:
Invoke-Elevated {echo "hi"} -AutoExit
Would that work for you?

wrote Jan 21, 2016 at 4:16 AM

felixfbecker wrote Jan 21, 2016 at 2:11 PM

Would it also work without the braces, like Invoke-Elevated -AutoExit echo "hi"?
But I see why you are doing this, it essentially prevents you from seeing the output of the command. I guess what I'm really looking for (and I guess a lot of Windows users) is sudo for Windows, so basically Invoke-Elevated but instead of spawning a new shell, only UAC pops up and then the command gets executed in the original shell. Is this possible somehow? Maybe spawn a new hidden shell and redirect output to the original? Maybe the name is a bit confusing as it suggests it does exactly that, simply invoke a command with elevated rights (not spawn a new shell).

wrote Nov 27 at 10:00 PM