Possible to open a PowerShell (run as Administrator) from Delphi?

108 views Asked by At

I want to open Powershell as an administrator (no problem if prompt for UAC to elevate) when i click to a button in Delphi.

Is it possible?

Thank you

1

There are 1 answers

3
Germán Estévez -Neftalí- On BEST ANSWER

There may be easier ways, but this one works.
Create a CMD/BAT file a write this:

@echo off
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
cd
@cd /d "%~dp0"
cd
powershell.exe

Save and execute it with Delphi using ShellExecute:

ShellExecute(Handle, 'open', PChar('r:\PowerShellUAC.bat'), nil, nil, SW_NORMAL);