Search code examples
powershelldelphidelphi-7

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


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


Solution

  • 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);