Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Run PowerShell commands as Administrator #366

Open
JakeD22 opened this issue Jun 25, 2024 · 1 comment
Open

[FEATURE] Run PowerShell commands as Administrator #366

JakeD22 opened this issue Jun 25, 2024 · 1 comment
Labels
features request Request for a new feature.

Comments

@JakeD22
Copy link

JakeD22 commented Jun 25, 2024

Is your feature request related to a problem? Please describe.
I have several commands in PowerShell that require to be ran as Administrator.

Describe the solution you'd like
A button or prompt to run as admin

Describe alternatives you've considered
I tried putting a command I got from ChatGPT in the inject PowerShell code that elevates the script but that didn't seem to work. Although the UAC prompt showed up which was promising but the code never ran. besides I don't want all code to be elevated if I can avoid it only code that needs elevation.
Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File "$PSCommandPath"" -Verb RunAs

Additional context
I did look around for this solution or question but couldn't find any, apologies if this has been asked before

Any and all help is appreciated, love this plugin. Saved me lots of work and time already

@JakeD22 JakeD22 added the features request Request for a new feature. label Jun 25, 2024
@Yetenol
Copy link

Yetenol commented Jan 12, 2025

Try putting this at the start of your code block

$is_executed_privileged = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).
    IsInRole([Security.Principal.WindowsBuiltInRole]::"Administrator")
if (-not $is_executed_privileged) {
    if ((Get-Command wt.exe) -ne $null) {
        Start-Process wt "powershell -File $PSCommandPath -ExecutionPolicy Bypass" -Verb RunAs
    } else {
        Start-Process powershell "-File $PSCommandPath -ExecutionPolicy Bypass" -Verb RunAs
    }
    Start-Sleep 10
    exit
}
"Hello World"
Read-Host

It failed before because the temporary script file $PSCommandPath gets deleted immediately after the code block script finishes, so before the elevated powershell can call it. Use Start-Sleep 10 to keep it running for some time, while the new terminal launches and calls the same file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
features request Request for a new feature.
Projects
None yet
Development

No branches or pull requests

2 participants