Search code examples
powershellcmdadminautoitprivileges

start .exe with stored admin password using cmd/powershell


I want to create a shortcut to all domain users when it is clicked, e.g. notepad++ in admin mode popup without asking users to input password. i.e.notepad++ in admin mode

Same effect as I told user the admin username and password and ask them right click notepad++ icon then enter username and password.

I tried following but it is not working.

  1. In cmd
"runas /savecred /user:{hostname}\admin "C:\Program Files\Notepad++\notepad++.exe"

It actually only starts notepad++ in normal mode, even I entered admin password.

I tried autoit, but since even running the above not starting notepad++ in admin mode, so it is not working too. I think sanur also not working.

  1. In powershell
Start-Process 'C:\Program Files\Notepad++\notepad++.exe' -Verb runAs

Elevated window pop up and asking to enter admin username and password, the notepad++ started is in admin mode, but I don't want the pop up. And I couldn't find a way pass in the username and password.

  1. In powershell
$username = "admin"
$password = "password"
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))

powershell Start-Process "C:\Program Files\Notepad++\notepad++.exe" -Credential ($credentials)

It actually only starts notepad++ in normal mode.


Solution

  • Thank you Theo for the comments.

    The solution is

    1. Create a shortcut and set it run as admin, eg, C:\temp\Notepad++.lnk

    By right-click the normal shortcut --> Advanced --> tick Run as administrator --> OK

    1. Create a .bat to start the shortcut eg, C:\temp\notepad.bat (we need this step because runas cannot start .lnk file) The .bat file here also written to avoid a cmd window popup when run
    
        @echo off
        @start "" "C:\temp\Notepad++.lnk"
    
    
    1. Create autoit script to run the .bat by runas, eg. notepad.au3
        RunAs ( "{adminAccount}", "{hostname}", "{adminPassword}", 1, "C:\temp\notepad.bat")
    
    1. Use autoit to turn au3 script to exe

    The notepad++ in admin mode will be started without any elevated window pop up to ask you for admin credential.