I need to run cmd on a button click as admin. It works. but I need to run it as an admin. How is this done?
ShellExecute(Handle, 'open', 'c:\Windows\system32\cmd.exe', nil, nil, SW_SHOWNORMAL)
You can open cmd as an administrator by searching for it in the Windows search bar located in the bottom left corner of the desktop screen. Then, right-click on Command Prompt and select Run as administrator.
In the search results window, under Programs, right-click on the program cmd.exe. In the pop-up menu, select Run As Administrator. If a User Access Control window appears, log in with a Windows user account that has full Administrator access rights. An Elevated Command Prompt window should now open.
To do so, search for Command Prompt in the Start menu, right-click the Command Prompt shortcut, and select Run as administrator. The Administrator user account is now enabled, although it has no password.
Replace the open
verb with the runas
as shown below. Anyway, try to avoid path hardcoding:
uses ShellAPI; procedure TForm1.Button1Click(Sender: TObject); begin ShellExecute(Handle, 'runas', 'cmd.exe', nil, nil, SW_SHOWNORMAL); end;
You can also add to your button the shield icon by setting the ElevationRequired
property to True.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With