title: NSIS Script Wrap to PS1 into EXE and Run as Admin
body: <p>I have the following NSIS (.nsi) script that wraps PowerShell script into exe.</p>
  
  <p>In addition, I would like the exe to run as admin because the scripts needs to update registry key.</p>
  
  <p>NSIS Script is:</p>
  
  <pre><code>!include x64.nsh
  
  RequestExecutionLevel admin ;Require admin rights on NT6+ (When UAC is turned on)
  OutFile "file.exe"
  SilentInstall silent
  
  Section
      SetOutPath $EXEDIR
      File "file.ps1"
      # Run the script to update
      ExecWait "powershell -ExecutionPolicy Bypass -WindowStyle Hidden -File .\file.ps1"
  SectionEnd
  
  Function .onInstSuccess
      Delete "file.ps1"
  FunctionEnd
  </code></pre>
  
  <p>PowerShell script is:</p>
  
  <pre><code>$registryPath = "HKLM:\SOFTWARE\Test"
  $Name = "keyName"
  $value = "keyValue"
  $preRegVer = (Get-ItemProperty $registryPath).Version
  #log "Pre registry value: $preRegVer"   
  If(!(Test-Path $registryPath))
  {
      # log "Path does not exist"
      New-Item -Path $registryPath -Force | Out-Null
      # log "Path created"
      New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType String -Force | Out-Null
      # log "Value created"
  }
  Else {
      # log "Path exist"
      $val = Get-ItemProperty -Path $registryPath
      if($val.Version -eq $null)
      {
          # log "Value does not exist"
          New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType String -Force | Out-Null
          # log "Value created"
      }
      Else {
          # log "Value exist"
          Remove-ItemProperty -path $registryPath -Name Version -Force
          # log "Value removed"
          New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType String -Force | Out-Null
          # log "Value created"
      } 
  }
  </code></pre>
  
  <p>When I run .exe file, it asks for elevated permission, but does not update the key.</p>
  
  <p>I know that powershell script works, because I used PowerGUI to compile it to exe, and it updates the key.</p>
  
  <p>Only problem with PowerGUI is that it does not have an option to run as Admin.</p>