Request admin privileges using nsis installer

When you want your nsis installer to make changes in some system folders or HKLM registry, your installer will need administrator privileges to do so.

Nsis makes it easier to request for admin privileges with a single line.

RequestExecutionLevel admin

You should also write a few more lines of code to make sure that escalation was successful, and quit the installer if escalation was unsuccessful. Following lines will do so:

Function .onInit
UserInfo::GetAccountType
pop $0
${If} $0 != “admin”
    MessageBox mb_iconstop “Administrator rights required!”
    SetErrorLevel 740 ;ERROR_ELEVATION_REQUIRED
    Quit${EndIf}

That’s all you have to write to get admin privilege in nsis installer!

If you don’t need admin privileges in your installer don’t forget to request for user privileges

RequestExecutionLevel user

If you forget to do so, Windows installer detection heuristics may detect your installer and automatically set the execution level to admin. 

Leave a Reply

Your email address will not be published. Required fields are marked *