Search code examples
installationcmdnsis

Execute a command using NSIS


i want to execute some commands using NSIS script , but for the commands to work i have to open command prompt using "run as administrator" from the right click menu . How do it do it using NSIS script .

I am using

 RequestExecutionLevel admin 

along with the exec command but this does not seem to work .


Solution

  • RequestExecutionLevel only works on Vista+ when UAC is on so you should also check at run-time to cover the other cases:

    Outfile RequireAdmin.exe
    RequestExecutionLevel admin ;Require admin rights on NT6+ (When UAC is turned on)
    
    !include LogicLib.nsh
    
    Function .onInit
    UserInfo::GetAccountType
    pop $0
    ${If} $0 != "admin" ;Require admin rights on NT4+
        MessageBox mb_iconstop "Administrator rights required!"
        SetErrorLevel 740 ;ERROR_ELEVATION_REQUIRED
        Quit
    ${EndIf}
    FunctionEnd
    
    Page InstFile
    
    Section
    SectionEnd